What are the lesser-known but useful features of the Python programming language?
- Try to limit answers to Python core.
- One feature per answer.
- Give an example and short description of the feature, not just a link to documentation.
- Label the feature using a title as the first line.
Quick links to answers:
- Argument Unpacking
- Braces
- Chaining Comparison Operators
- Decorators
- Default Argument Gotchas / Dangers of Mutable Default arguments
- Descriptors
- Dictionary default
.get
value - Docstring Tests
- Ellipsis Slicing Syntax
- Enumeration
- For/else
- Function as iter() argument
- Generator expressions
import this
- In Place Value Swapping
- List stepping
__missing__
items- Multi-line Regex
- Named string formatting
- Nested list/generator comprehensions
- New types at runtime
.pth
files- ROT13 Encoding
- Regex Debugging
- Sending to Generators
- Tab Completion in Interactive Interpreter
- Ternary Expression
try/except/else
- Unpacking+
print()
function with
statement
If you don't like using whitespace to denote scopes, you can use the C-style {} by issuing:
Creating generators objects
If you write
you can get out the generator and assign it to x. Now it means you can do
The advantage of this is that you don't need intermediate storage, which you would need if you did
In some cases this can lead to significant speed up.
You can append many if statements to the end of the generator, basically replicating nested for loops:
Interactive Interpreter Tab Completion
You will also have to set a PYTHONSTARTUP environment variable.
Context managers and the "
with
" StatementIntroduced in PEP 343, a context manager is an object that acts as a run-time context for a suite of statements.
Since the feature makes use of new keywords, it is introduced gradually: it is available in Python 2.5 via the
__future__
directive. Python 2.6 and above (including Python 3) has it available by default.I have used the "with" statement a lot because I think it's a very useful construct, here is a quick demo:
What's happening here behind the scenes, is that the "with" statement calls the special
__enter__
and__exit__
methods on the file object. Exception details are also passed to__exit__
if any exception was raised from the with statement body, allowing for exception handling to happen there.What this does for you in this particular case is that it guarantees that the file is closed when execution falls out of scope of the
with
suite, regardless if that occurs normally or whether an exception was thrown. It is basically a way of abstracting away common exception-handling code.Other common use cases for this include locking with threads and database transactions.
Exception else clause:
The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try ... except statement.
See http://docs.python.org/tut/node10.html
Be careful with mutable default arguments
Instead, you should use a sentinel value denoting "not given" and replace with the mutable you'd like as default: