Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 in 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?

like image 929
Chris B. Avatar asked Jan 21 '10 20:01

Chris B.


5 Answers

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.

like image 159
FogleBird Avatar answered Oct 31 '22 19:10

FogleBird


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.

like image 18
Fabio Zadrozny Avatar answered Oct 31 '22 21:10

Fabio Zadrozny


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

like image 6
Gustavo Labegalini Avatar answered Oct 31 '22 19:10

Gustavo Labegalini


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?"

like image 3
Albert Visser Avatar answered Oct 31 '22 20:10

Albert Visser


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"...

like image 3
Steve Carter Avatar answered Oct 31 '22 20:10

Steve Carter