default values in tuple assignment?

S

sosman

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.
 
D

Duncan Booth

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())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top