Showing posts with label python 3. Show all posts
Showing posts with label python 3. Show all posts

February 18, 2010

A Python 3 Idiom?

A while ago I wrote a short Python 3 snippet as an introduction to a post about factory functions. Second thoughts are proverbially the best, and I realised that in the following code:

def powers(a):
    return a, a*a, a*a*a

a, square, cube = powers(10)
print(a, square, cube)

the last two lines could be refactored as:
print(*powers(10))

This technique is ineffective in Python 2, where print is a statement and the syntax would be invalid.