【问题标题】:python 3.3.2 function definition multi parameter syntax [duplicate]python 3.3.2函数定义多参数语法[重复]
【发布时间】:2013-11-21 21:39:14
【问题描述】:

我有一个类似这样的代码:

a=1.
b=2.

c=(a,b)

def test((a,b),c):
    return a+b+c

test(c,5)

但是,它说第二个括号中有语法错误:def test((a,b),c)

有什么建议吗? (顺便说一句,这适用于 2.6.1,我有 3.3.2,我找不到任何关于此的语法更改)

【问题讨论】:

    标签: python python-3.x syntax


    【解决方案1】:

    该特性——元组参数解包——已从 Python 3 中移除:http://www.python.org/dev/peps/pep-3113/

    你应该重写你的代码:

    def test(a, b, c):
        return a + b + c
    
    test(c[0], c[1], 5)
    

    def test(a, b):
        return a[0] + a[1] + b
    
    test(c, 5) 
    

    【讨论】:

    • 您提供的链接非常有帮助,谢谢。
    【解决方案2】:

    来自What’s New In Python 3.0

    元组参数解包已删除。不能再写def foo(a, (b, c)): ....,改用def foo(a, b_c): b, c = b_c

    相关 PEP:PEP 3113

    【讨论】:

      猜你喜欢
      • 2013-11-03
      • 2012-03-14
      • 2016-04-13
      • 2020-04-01
      • 2019-04-14
      • 2018-09-14
      • 2015-10-19
      • 2021-07-20
      • 1970-01-01
      相关资源
      最近更新 更多