python 如何修复 PyDev“方法应该将 self 作为第一个参数”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2112770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fix PyDev "Method should have self as first parameter" errors
提问by Chris B.
I'm developing in Python using PyDev in Eclipse, and some of my code generates errors in the code analysis tool. Specifically:
我在 Eclipse 中使用 PyDev 在 Python 中进行开发,我的一些代码在代码分析工具中生成错误。具体来说:
class Group(object):
def key(self, k):
class Subkey(object):
def __enter__(s):
self._settings.beginGroup(k)
return self
def __exit__(s, type, value, tb):
self._settings.endGroup()
return Subkey()
Gives me a "Method '__enter__- group' should have self as first parameter"
error, and a similar error for __exit__
. Is there a way to solve this without assigning self
to another variable and reusing the variable in the other method signatures?
给我一个"Method '__enter__- group' should have self as first parameter"
错误,以及类似的错误__exit__
。有没有办法在不分配self
给另一个变量并在其他方法签名中重用该变量的情况下解决这个问题?
回答by FogleBird
You could disable that error in the preferences...
您可以在首选项中禁用该错误...
Window > Preferences > Pydev > Editor > Code Analysis > Others
Or refactor the code...
或者重构代码...
class Group(object):
def key(self, k):
outer_self = self
class Subkey(object):
def __enter__(self):
outer_self._settings.beginGroup(k)
return outer_self
def __exit__(self, type, value, tb):
outer_self._settings.endGroup()
return Subkey()
What else do you expect? The error checks are there to help you. If you don't think they're legitimate errors, disable them or refactor the code.
你还有什么期待?错误检查可以帮助您。如果您认为它们不是合法错误,请禁用它们或重构代码。
In this case I'd say refactor the code. It's more readable, as evidenced by King Radical's answer. He didn't understand that s
was another self
.
在这种情况下,我会说重构代码。正如King Radical的回答所证明的那样,它更具可读性。他不明白那s
是另一个self
。
回答by Fabio Zadrozny
Using Ctrl+1
in a line with an error from PyDev will always bring you a fix which will allow you to ignore the PyDev error in the line. In this specific case, it'll allow you to ignore the error by adding #@NoSelf
to the end of the line. Ctrl+1
is also useful when some unused import is needed and under other situations.
使用Ctrl+1
与来自PyDev的一个错误的行总是给你带来的修复,这将让你忽略行PyDev的错误。在这种特定情况下,它将允许您通过添加#@NoSelf
到行尾来忽略错误。Ctrl+1
当需要一些未使用的导入时和在其他情况下也很有用。
回答by Steve Carter
IMO this is a silly warning. the name"self" is only convention. I got the habit of using the name "_" to allow the member names to be more obvious,
IMO 这是一个愚蠢的警告。“自我”这个名字只是约定俗成。我养成了使用名称“_”的习惯,让成员名称更明显,
class myClass( object ):
def __init__( _, color, shape, weight ):
_.color=color
_.shape=shape
_.weight=weight
...
and I get this warning all over my library of thousands of lines of code. So I'll be switching this warning off. Would be nice to be able to specify "for this project I use '_' by convention"...
我的数千行代码库中到处都是这个警告。所以我将关闭这个警告。能够指定“对于这个项目我按照惯例使用'_'”会很好......
回答by Gustavo Labegalini
You can use a decorator:
您可以使用装饰器:
class aClass:
def __init__(self): # instance-dependent method
self.atribite1 = []
self.atribute2 = 0
@staticmethod
def static(): # static method
pass
回答by GreenMatt
PyDev is telling you that Python class methods must have self
as the first variable they receive, if they're going to access the class member variables. See: http://www.python.org/doc/faq/general/#why-must-self-be-used-explicitly-in-method-definitions-and-calls
PyDev 告诉您 Python 类方法必须将其self
作为它们接收的第一个变量,如果它们要访问类成员变量。请参阅:http: //www.python.org/doc/faq/general/#why-must-self-be-used-explicitly-in-method-definitions-and-calls
Edit: It didn't initially occur to me that you might be using s
instead of self
, but in view of the other answers, that may be. However, if you're going to do that, you also need to use s
as your variable in the method, rather than self
.
编辑:我最初没有想到您可能会使用s
代替self
,但鉴于其他答案,可能是。但是,如果您打算这样做,您还需要s
在方法中使用作为变量,而不是self
.
回答by Albert Visser
It shouldn't be an error in the first place, as using "self" is only a widely-accepted convention. It should be a warning at most, in the sense of "are you sure you're using the class instance as the first argument?"
首先它不应该是一个错误,因为使用“self”只是一个被广泛接受的约定。从“您确定将类实例用作第一个参数吗?”的意义上来说,它最多应该是一个警告?