The first time you try to write a network client or server directly on top of sockets, you do something like this:
for filename in filenames: with open(filename, 'rb') as f: sock.sendall(f.read())
And then, on the other side:
for i in count(0): msg = sock.recv(1<<32) if not msg: break with open('
for filename in filenames: with open(filename, 'rb') as f: sock.sendall(f.read())
And then, on the other side:
for i in count(0): msg = sock.recv(1<<32) if not msg: break with open('
(Someone pointed out to me that Ned Batchelder has a similar post called Keep data out of your variable names.
Novices to Python often come up with code that tries to build and evaluate strings, like this:
for name in names: exec('{} = {}'.format(name, 0))
… or this:
exec('def func(x): return {} * x**2 + {} * x + {}'.format(a, b, c)) return func
99% of the time, this is a problem you shouldn't be solving
for name in names: exec('{} = {}'.format(name, 0))
… or this:
exec('def func(x): return {} * x**2 + {} * x + {}'.format(a, b, c)) return func
99% of the time, this is a problem you shouldn't be solving
If you look at the major changes in Python 3, other than the Unicode stuff, most of them are about replacing list-based code with iterator-based code.
Let's say you've got the list [-10, 3, -2, 14, 5], and you want the list filtered to just the non-negative values.