Open dopplershift opened 9 years ago
Similar, but from a Raymond Hettinger talk (iter
taking a sentinel was already on my list). This:
blocks = []
while True:
block = f.read(32)
if block == '':
break
blocks.append(block)
can be turned into:
blocks = []
for block in iter(partial(f.read, 32), ''):
blocks.append(block)
And really:
blocks = list(iter(partial(f.read, 32), ''))
And that iter
construct can be passed to things like sorted
, min
, max
, etc.
Linking Ned Batchelder's talk Loop like a native so I don't lose it--it's another good reference for these kind of things.
Also:
.keys()
rather than looping over dict directly.defaultdict
vars()
instead of reading class __dict__
ChainMap
to replace combining a bunch of dicts togetherdel l[0]
, l.pop(0)
, l.insert(0, item)
ignored
context manager to ignore exceptions
Been reading effective Python. Some things that might need reviewing:
enumerate(container, start)
(will add others as I keep reading)