default values in tuple assignment?

Duncan Booth me at privacy.net
Tue Jul 20 13:04:40 CEST 2004


sosman <news01 at metrak.KILLSPAM.com> wrote in 
news:40fcec24$0$4754$afc38c87 at news.optusnet.com.au:

> I take it python doesn't support defaults when assigning to a tuple, eg:
> 
> for line in file:
>      (parameter, value, units = 'kg') = line.split()
> 
> along the lines of default parameter assignment in function calls.
> 

You take it correctly. It isn't too hard to get a similar effect though:

parameter, value, units = (line.split() + ['kg',])[:3]

Of course, if the line splits into more than 3 words this version fails to 
throw any kind of error. If you want an error to be thrown, how about:

def pvu(parameter, value, units='kg'):
    return parameter, value, units
parameter, value, units = pvu(*line.split())



More information about the Python-list mailing list