How do I fix PyDev "Method should have self as first parameter" errors
I'm developing in Python using PyDev in Eclipse, and some of my code generates errors i开发者_运维技巧n the code analysis tool. Specifically:
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?
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
.
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.
You can use a decorator:
class aClass:
def __init__(self): # instance-dependent method
self.atribite1 = []
self.atribute2 = 0
@staticmethod
def static(): # static method
pass
The Built-in function used for this
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?"
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,
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"...
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
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
.
精彩评论