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
Doctest: documentation and unit-testing at the same time.
Example extracted from the Python documentation:
Decorators
Decorators allow to wrap a function or method in another function that can add functionality, modify arguments or results, etc. You write decorators one line above the function definition, beginning with an "at" sign (@).
Example shows a
print_args
decorator that prints the decorated function's arguments before calling it:Descriptors
They're the magic behind a whole bunch of core Python features.
When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the
__get__
,__set__
, or__delete__
methods.Here's how you'd implement your own (read-only) version of property using descriptors:
and you'd use it just like the built-in property():
Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.
Raymond Hettinger has an excellent tutorial that does a much better job of describing them than I do.
Chaining comparison operators:
In case you're thinking it's doing
1 < x
, which comes out asTrue
, and then comparingTrue < 10
, which is alsoTrue
, then no, that's really not what happens (see the last example.) It's really translating into1 < x and x < 10
, andx < 10 and 10 < x * 10 and x*10 < 100
, but with less typing and each term is only evaluated once.Function argument unpacking
You can unpack a list or a dictionary as function arguments using
*
and**
.For example:
Very useful shortcut since lists, tuples and dicts are widely used as containers.
Nested list comprehensions and generator expressions:
These can replace huge chunks of nested-loop code.