I’ve seen some Python functions written like this:
def get_year((year,prefix,index,suffix)):
return year
How does that differ (if at all) from other functions without the extra parentheses like this:
def do_format(yr,pfx,id,sfx):
return "%s %s %s/%s"%(yr, id, pfx, sfx)
Or is it just a matter of taste in styles, or if they differ, can get_year() be rewritten to be in the style of do_format() or vice-versa without effecting existing caller’s syntax?
The
get_yearfunction in your example uses an automatically unpacked tuple parameter (this is the feature gone from Python 3). To call it, you give it a single parameter, and that parameter is expected to be a sequence containing four values.To rewrite this for Python 3 but not alter the invocation (in other words, to not break existing code which calls
get_year):The above is essentially what tuple unpacking is doing for you automatically. In this particular case, you could simply write
For further information, read PEP 3113.