Missing values in tuple assignment

J

Jim Garrison

Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments. I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:
'is the time'

However, if the optional argument string is missing:
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
x,y=a.split(None,1)
ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple. Is there a way to get the assignment to default the
missing values to None?
 
A

Albert Hopkins

Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments. I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:
'is the time'

However, if the optional argument string is missing:
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
x,y=a.split(None,1)
ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple. Is there a way to get the assignment to default the
missing values to None?

why not do this?
>>> a= 'now'
>>> z = a.split(None, 1)
>>> x = z[0]
>>> y = z[1] if len(z) == 2 else None
 
M

MRAB

Albert said:
Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments. I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:
a="now is the time"
x,y=a.split(None,1)
x 'now'
y
'is the time'

However, if the optional argument string is missing:
a="now"
x,y=a.split(None,1)
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
x,y=a.split(None,1)
ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple. Is there a way to get the assignment to default the
missing values to None?

why not do this?
a= 'now'
z = a.split(None, 1)
x = z[0]
y = z[1] if len(z) == 2 else None
A 1-line solution (not necessarily recommended) is:

x, y = (a.split(None, 1) + [None])[ : 2]
 
T

Terry Reedy

Jim said:
Use case: parsing a simple config file line where lines start with a
keyword and have optional arguments. I want to extract the keyword and
then pass the rest of the line to a function to process it. An obvious
use of split(None,1)

cmd,args= = line.split(None,1);
if cmd in self.switch: self.switch[cmd](self,args)
else: self.errors.append("unrecognized keyword '{0)'".format(cmd))

Here's a test in IDLE:
'is the time'

However, if the optional argument string is missing:
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
x,y=a.split(None,1)
ValueError: need more than 1 value to unpack

I understand the problem is not with split() but with the assignment
to a tuple. Is there a way to get the assignment to default the
missing values to None?

In 3.0 (and 2.6?), the following is close:
>>> a,*b = (1,2)
>>> a,b (1, [2])
>>> a,*b = (1,)
>>> a,b
(1, [])
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top