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 first function takes a single tuple argument, whereas the second function takes 4 arguments. You can pass those parameters individually, or as a tuple with splat
operator, that will unpack the tuple into individual parameters.
E.g:
# Valid Invocations
print do_format(*('2001', '234', '12', '123')) # Tuple Unpacking
print do_format('2001', '234', '12', '123') # Separate Parameter
print get_year(('2001', '234', '12', '123'))
# Invalid invocation.
print do_format(('2001', '234', '12', '123')) # Passes tuple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With