方法(a,b ='',* c,** d):得到语法错误? [英] method (a, b = '', *c, **d): gets a syntax error?

查看:89
本文介绍了方法(a,b ='',* c,** d):得到语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我对Python比较陌生,我对一些

代码有一个奇怪的问题。在类中,__ call__方法获取如下参数:

class WhatsUp:

__call__(

self,

var1,

var2 ='''',

* moreVars,

** moreNamedVars,

):

通过

我总是得到** moreNamedVars的错误,其中''^''指向

在逗号处的逗号结束(或者,如果我删除逗号,它指向

冒号)。我还尝试将传递给

方法的其他变量注释掉。相同的结果。

我假设变量的顺序和变量的变量列表必须是这样的?b $ b?有什么不对?

亲切的问候


安德烈亚斯

解决方案

< blockquote> Andreas Neudecker写道:

你好。

我对Python比较新,我对一些代码有一个奇怪的问题。在类中,__ call__方法获取如下参数:

class WhatsUp:
__call __(




你错过了关键字''def''在这里,就在方法名称之前。

Alex


Andreas Neudecker< a。** *******@uni-bonn.de>写道:

你好。

我对Python比较新,我有一个奇怪的问题使用一些
代码。在类中__call__方法获取如下参数:

class WhatsUp:
__call__(
self,
var1,
var2 ='''',
* moreVars,
** moreNamedVars,
):
传递

我总是得到一个错误** moreNamedVars,行''^''
指向末尾的逗号(或者,如果我删除逗号,它指向结尾处的逗号)。我也尝试评论出来其他变种bles传递给了方法。相同的结果。
我假设变量的顺序和变量的变量列表是这样的?有什么问题?




缺少一个def?

干杯,

mwh

-

任何形式的邪恶都可以在没有*太多努力的情况下被发现

是值得的......我没有想知道我们正在寻找什么样的邪恶

这里或如何检测是,所以我不能回答是或否。

- Guido Van Rossum ,python-dev




" Andreas Neudecker" <一个********* @ uni-bonn.de>。在消息中写道

news:3F ************** @ uni-bonn.de ...

class WhatsUp:
__call__(
self,
var1,
var2 ='''',
* moreVars,
** moreNamedVars,
):
传递

我总是得到** moreNamedVars的错误,其中''^''
指向末尾的逗号(或者,如果我删除逗号,它指向
冒号)。 ......出了什么问题?




通常,在报告''我收到错误'时,你应该复制实际的

错误信息。在这种情况下,我认为你的两个语法错误都有''SyntaxError:invalid

syntax''。


1.当你做一个函数调用和使用**无论如何,它必须是参数列表中的最后一项

,就像在函数定义中一样。对于defs或调用,不允许使用逗号后面的
。所以当你

添加''def''来纠正你的实际错误时,你还需要省略

逗号。


2. defs需要冒号后缀,但禁止使用冒号。

使用逗号时,解析器永远不会达到此目的。

与大多数批处理编译器不同, CPython解析器退出它看到的第一个

错误。因此,当您更正一个错误并重新提交时,您可能会在代码中进一步公开另一个错误。


欢迎使用Python。享受。


Terry J. Reedy


Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (
self,
var1,
var2 = '''',
*moreVars,
**moreNamedVars,
):
pass
I always get an error for the **moreNamedVars, line where the ''^'' points
at the comma at the end (or, if I remove the comma, it points at the
colon). I also tried commenting-out the other variables passed to the
method. Same result.
I assumed the order of variables and variable lists of variables had to
be like this? What is wrong?
Kind regards

Andreas

解决方案

Andreas Neudecker wrote:

Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (



You''re missing the keyword ''def'' here, right before the method name.
Alex


Andreas Neudecker <a.*********@uni-bonn.de> writes:

Hello.

I am relatively new to Python and I have a strange problem with some
code. In a class the __call__ method gets parameters like this:
class WhatsUp:
__call__ (
self,
var1,
var2 = '''',
*moreVars,
**moreNamedVars,
):
pass
I always get an error for the **moreNamedVars, line where the ''^''
points at the comma at the end (or, if I remove the comma, it points
at the colon). I also tried commenting-out the other variables passed
to the method. Same result.
I assumed the order of variables and variable lists of variables had
to be like this? What is wrong?



Missing a def?

Cheers,
mwh

--
Any form of evilness that can be detected without *too* much effort
is worth it... I have no idea what kind of evil we''re looking for
here or how to detect is, so I can''t answer yes or no.
-- Guido Van Rossum, python-dev



"Andreas Neudecker" <a.*********@uni-bonn.de> wrote in message
news:3F**************@uni-bonn.de...

class WhatsUp:
__call__ (
self,
var1,
var2 = '''',
*moreVars,
**moreNamedVars,
):
pass
I always get an error for the **moreNamedVars, line where the ''^'' points at the comma at the end (or, if I remove the comma, it points at the
colon). ...What is wrong?



Generaly, when reporting ''I got an error'', you should copy the actual
error message. In this case, I presume you got ''SyntaxError: invalid
syntax'' for each of your two syntax errors.

1. When you make a function call and use **whatever, it must be the
last item in the argument list, just as in a function definition. A
following comma is not allowed for either defs or calls. So when you
add ''def'' to correct your actual error, you also need to omit the
comma.

2. The colon suffix is required for defs but forbidden for calls.
With the comma present, the parser never got far enough to this.
Unlike most batch compilers, the CPython parser quits at the first
error it sees. So when you correct one error and resubmit, you may
expose another further in your code.

Welcom to Python. Enjoy.

Terry J. Reedy


这篇关于方法(a,b ='',* c,** d):得到语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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