470,647 Members | 1,155 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

default values in tuple assignment?

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.
Jul 18 '05 #1
1 4106
sosman <ne****@metrak.KILLSPAM.com> wrote in
news:40**********************@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())
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.