In the series of the Python « features » that makes me happy last time i began with two concepts, the with statement and the list comprehensions, now i’m going to talk about Multiple assignments and the import aliases.
- Multiple assignments
It’s a simple idea that lets you return a series of value and on the other end assign those multiple values at the same time, example when you’re splitting a string or extracting groups from a regular expression :
>>> split_me ="here,we, are,again" # splitting we'll get a list of values >>> split_me.split(",") ['here', 'we', ' are', 'again'] # if you don't know the number of values you're going to have # you can't use this features, example : >>> (start,end) = split_me.split(",") Traceback (most recent call last): File "", line 1, in ValueError: too many values to unpack # but if you know that there's going to be n values : >>> (start,end) = split_me.split(" ") >>> start 'here,we,' >>> end 'are,again'
- Import aliases
It means what it says, when you import a library or module, you can use aliases, example in Django for shortcuts :
# this is extracted from my own code : from django.http import HttpResponse, HttpResponseRedirect as redirect from django.shortcuts import render_to_response as render</pre>
I won’t start to talk about the standard library as a whole, or libs like Numpy, Scypy, scikit-learn, …. that makes it so easy to just think in Python.
And you ? What are the Python constructs (2.x or 3.x) that makes you feel happy and efficient at the end of the day ?