Profile photo for Tracy Chou

Caveat: "Time saving" is a loose term -- especially for a large and/or growing codebase shared by many developers, you want to be careful that time saved by one person in writing code (e.g. with clever one-liners) doesn't grossly compromise time spent by other people in reading it, maintaining it, or otherwise working with it. This answer addresses the question in terms of broader productivity gains across a software team, as opposed to tips and tricks to hack up throwaway Python code as quickly as possible.

  • For idiomatic Python, get comfortable with list comprehensions, generators, decorators, and context managers.
    --
    List comprehensions are often the most idiomatic way of creating and transforming lists.
    --
    Generators are most often used for defining iterators cleanly, usually with less memory overhead than alternative solutions.
    --
    Decorators are great for common setup/cleanup code, like authentication, caching, or timing. Some built-in decorators like @classmethod and @property are very useful when working with classes. See also: What are common uses of Python decorators?
    --
    Context managers are also handy for setup/cleanup code, for example in opening and closing files or tags (Quora uses these to make sure tags in HTML generated from WebNode are always closed). Another cool use case is setting some state for the duration of a code block, like flags for bypassing cache or for pipelining networked instructions.
  • Familiarize yourself with all the methods of the most common data structures: lists, sets, dictionaries. Python has a lot of useful built-in functionality for working with these data structures, like slicing for lists or mathematical set operations with syntactic sugar for easy use on sets.
  • Run code snippets at an iPython shell. Take full advantage of the fact that Python has a great interactive shell -- test out your code snippets quickly and fiddle with syntax or logic 'til they work, inspect your objects and functions, time function calls, etc. Make sure to use iPython and not the standard Python shell, as iPython is much more powerful and has nice features like tab-completion and %history.
  • Use a linter like PyFlakes, PyLint, or PyChecker (comparison here: http://stackoverflow.com/questions/1428872/pylint-pychecker-or-pyflakes). Better yet, hook your linter into your editor so it checks as you go, similar to the code analysis and automatic error or warning highlighting that Java IDEs do.
  • Familiarize yourself with Python's standard libraries, like urllib2, time and datetime, random, itertools, functools, re, collections, etc. See also: http://stackoverflow.com/questions/1453952/most-useful-python-modules-from-the-standard-library
  • Use print statements and pdb to debug. print is particularly effective to use in Python since you don't need to compile your code again to start printing useful debug output. As for pdb, just dump this wherever you need to start investigating your running program: import pdb; pdb.set_trace(); and step through/in, continue, or print values as you please. See also: http://docs.python.org/library/pdb.html
  • Adopt strong naming conventions to make your code more self-documenting. For example, if you have user ids and user objects in your codebase, always use *_user_id to refer to the former and *_user to refer to the latter, so it's always clear whether you're working with an int/long that is the id or an object that is the full user object. (This is an example that is particularly salient given Python's lack of static type annotations.) And unless you are using _ for i18n, use _ to indicate values to be thrown away.
View 14 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025