0
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?

5
Contributors
6
Replies
83
Views
2 Years
Discussion Span
Last Post by woooee
2

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:]

Edited by Gribouillis

Votes + Comments
awesome :D
4

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]
0

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

Edited by vegaseat

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.