Transforming Code into Beautiful, Idiomatic Python

Presenter: Raymond Hettinger

Track: VI

Description:

Learn to take better advantage of Python’s best features and improve existing code through a series of code transformations, “When you see this, do that instead.”

Examples:

for x in range(len(colors)):
    print colors[x]

# Should be
for x in colors:
    print colors


n = min(len(names), len(colors))
for i in range(n):
    print names[i], '-->', colors[i]

# Should be
for name, color in zip(names, colors):
    print name, '-->', color

# But zip is slow, so you should use izip:
for name, color in izip(names, colors):
    print name, '-->', color

Awesome components:

  • zip/izip
  • defaultdict
  • Chain
  • deque
  • localcontext
  • with open() as f:
  • with ignored(SpecificException): do something that could throw an exception

Project Versions

Previous topic

Python Profiling

Next topic

Who’s there? - Home Automation with Arduino/RaspberryPi

This Page