为什么拆开元组会导致语法错误? [英] Why does unpacking a tuple cause a syntax error?

查看:95
本文介绍了为什么拆开元组会导致语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我这样写:

In Python, I wrote this:

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

我正在尝试将bvar扩展为函数调用作为参数. 但随后返回:

I'm trying to expand bvar to the function call as arguments. But then it returns:

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?

推荐答案

如果要将最后一个参数作为(mnt, False, bvar[0], bvar[1], ...)的元组传递,则可以使用

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


扩展调用语法*b仅可用于调用函数函数参数


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

这篇关于为什么拆开元组会导致语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆