records = [
        ('foo',1,2),
        ('bar','hello'),
        ('foo',3,4),
        ]

def do_foo(x,y):
    print 'foo\t{0}{1}'.format(x,y)

def do_bar(s):
    print 'bar\t{0}'.format(s)

for tag , *args in records:
    if tag == 'foo':
        do_foo(*args)
    elif tag == 'bar':
        do_bar(*args)

when i run the above program , i get invalid syntax .I dono why this happens? Can someone help me please?

Recommended Answers

All 6 Replies

I think it works only with recent versions of python. For me, it works with python 3.4 but not with 2.7.

Edit: you can write

for rec in records:
    tag, args = rec[0], rec[1:]
commented: awesome :D +6

Advanced unpacking only works with Python3. Here is an example ...

# advanced unpacking ...
a, b, *rest = range(10)
print(a)     # 0
print(b)     # 1
print(rest)  # [2, 3, 4, 5, 6, 7, 8, 9]

I can't solve it.

You can't solve what?

@MIOCUNARD,
let's hope you are not building yourself up to be one of those signature spammers.

What else could it be?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.