views:

72

answers:

3

Hello, In python, I wrote this:

bvar=mht.get_value()
temp=self.treemodel.insert(iter,0,(mht,False,*bvar))

I'm trying to expand bvar to the funtion call as arguments. But then it return,

File "./unobsoluttreemodel.py", line 65
    temp=self.treemodel.insert(iter,0,(mht,False,*bvar))
                                                 ^
SyntaxError: invalid syntax

What just happen? It should be correct right?

A: 

You appear to have an extra level of parentheses in there. Try:

temp=self.treemodel.insert(iter,0,mht,False,*bvar)

Your extra parentheses are trying to create a tuple using the * syntax, which is a syntax error.

Greg Hewgill
+5  A: 

If you want to pass the last argument as a tuple of (mnt, False, bvar[0], bvar[1], ...) you could use

temp = self.treemodel.insert(iter, 0, (mht,False)+tuple(bvar) )

The extended call syntax *b can only be used in calling functions, function arguments, and tuple unpacking on Python 3.x.

>>> def f(a, b, *c): print(a, b, c)
... 
>>> x, *y = range(6)
>>> f(*y)
1 2 (3, 4, 5)

Tuple literal isn't in one of these cases, so it causes a syntax error.

>>> (1, *y)
  File "<stdin>", line 1
SyntaxError: can use starred expression only as assignment target
KennyTM
Right, the `*` resolution operator is not allowed for creating tuples.
AndiDog
+1  A: 

Not it isn't right. Parameters expansion works only in function arguments, not inside tuples.

>>> def foo(a, b, c):
...     print a, b, c
... 
>>> data = (1, 2, 3)
>>> foo(*data)
1 2 3

>>> foo((*data,))
  File "<stdin>", line 1
    foo((*data,))
         ^
SyntaxError: invalid syntax
kriss