theano 出现奇怪的语法错误

Strange syntax error with theano

我现在正在 python 3.4 中尝试在我的代码中使用 theano。然而,有许多函数具有以下奇怪的语法

def c_code(self, node, name, (var1, var2), (var3,), sub):
...

即它们在函数定义中有括号。

Python 对它们抛出语法错误

File ".../Theano-0.7.0/theano/scalar/basic.py", line 1011
def c_code(self, node, name, (var1, var2), (var3, ), sub):
^ SyntaxError: invalid syntax

现在,一旦我删除了那些额外的括号,一切都很好,但我是 python 的新手,并且注意到 python 3 中有很多更改,因此这些括号可能需要用其他东西代替而不是删除。

谁能给我解释一下 (a) 函数定义中有括号是什么意思?和 (b) 是否以及如何使这些与 python 3 一起工作?

元组参数解包已在 Python 3.0 中通过 PEP3113:

移除

Tuple parameter unpacking is the use of a tuple as a parameter in a function signature so as to have a sequence argument automatically unpacked. An example is:

def fxn(a, (b, c), d):
    pass

The use of (b, c) in the signature requires that the second argument to the function be a sequence of length two (e.g., [42, -13] ). When such a sequence is passed it is unpacked and has its values assigned to the parameters, just as if the statement b, c = [42, -13] had been executed in the parameter.

Unfortunately this feature of Python's rich function signature abilities, while handy in some situations, causes more issues than they are worth. Thus this PEP proposes their removal from the language in Python 3.0.

所以如果你采用这个函数签名

def fun(foo, (a, b, c), bar):
    pass

那么相当于

def fun(foo, arg, bar):
    a, b, c = arg
    pass

所以这就是您使用 Python 3.x.

实现相同行为的方式

但是,由于这不是您自己的代码库,我看不到解决该问题的简单方法(缺少猴子补丁),并且可能还有更多 Python 3 个不兼容问题t 和 SyntaxErrors.

一样容易被发现

有趣的是,issue #783 linked by @tobias_k has been closed, and from it it seems that Python 3 support has been addressed and finished. Also, Theano does claim Python 3 support according to it's Trove classifiers

但是,您使用的版本(0.7.0)是最新发布的版本,您遇到的函数签名实际上是still to be found on current master. So - this is a bug, you should probably file an issue on Theano's GitHub issue tracker.