Advanced Python Features

Vax
198.4K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Python is full of awesome features and tricks, that make you think "Wow! Python is so cool!".

We've done a selection of features we particularly like. We hope you'll learn something that will make you say "Neat! I didn't know that".

The source code is on GitHub, please feel free to come up with ideas to improve it.

Generators

A generator is an object that produces a sequence of values. It can be used as an iterator, which means that you can use it with a for statement, or use the next function to get the next value. However, you can iterate over the values only once.

A generator can be created using a function that uses the yield keyword to generate a value. When a generator function is called, a generator object is created.

yield operator

For simple cases, it is possible to create a generator using a generator expression. As opposed to a list, the values will be computed on the fly instead of being computed once and stored in memory. Learn more about list and generator expressions.

generator expressions

Collections Module

collections is a module in the standard library that implements alternative container datatypes.

For example, a Counter is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values:

Counter

A defaultdict is a subclass of dict, which allows to pass a factory used to create automatically a new value when a key is missing.

defaultdict

The defaultdict can be used to create a tree data structure!

Tree

Itertools Module

itertools is a module in the standard library that allows you to create iterators for efficient looping.

For example, permutations allows you to generate all the possible ways of ordering a set of things:

permutations

Similarly, combinations generates all the possible ways of selecting items from a collection, such that (unlike permutations) the order does not matter:

combinations

itertools also contains utility functions such as chain, which takes iterables and creates a new iterator that returns elements from the given iterables sequentially, as a single sequence:

chain

Packing / Unpacking

The * operator, known as the unpack or splat operator allows very convenient transformations, going from lists or tuples to separate variables or arguments and conversely.

Extended Iterable Unpacking

When the arguments for your function are already in a list or in a tuple, you can unpack them using *args if it's a list, or **kwargs if that's a dict.

unpacking arguments

The opposite is also possible, you can define a function that will pack all the arguments in a single tuple and all the keyword arguments in a single dict.

keyword arguments

Decorators

A decorator is simply a function which takes a function as a parameter and returns a function.

For example, in the following code, the cache function is used as a decorator to remember the Fibonacci numbers that have already been computed:

decorators

The functools module provides a few decorators, such as lru_cache which can do what we just did: memoization. It saves recent calls to save time when a given function is called with the same arguments:

lru_cache

Context Managers

Context managers are mainly used to properly manage resources. The most common use of a context manager is the opening of a file: with open('workfile', 'r') as f:. However most developers have no idea how that really works underneath nor how they can create their own.

Actually, a context manager is just a class that implements the methods __enter__ and __exit__.

Context Manager

For simple use cases, it's also possible to use a generator function with a single yield, using the @contextmanager decorator.

Context Manager Using @contextmanager

Voilà! We hoped you enjoyed our selection of best features in Python 3, feel free to share your feedback on the forum or on our Github :)

There is a great demand for other playgrounds about async / await, unit testing or metaclasses. Create your own playground and we will add a link to it.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content