views:

305

answers:

5

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?

A: 
def __enter__(self):

and

def __exit__(self, type, value, tb):

Class member methods always need to have their first parameter be self

KingRadical
And what, pray tell, would that accomplish, other than fixing the PyDev code analysis error at the cost of breaking the code?
Chris B.
The parameter that receives self does not have to be named self. It could be named s, and it will contain a reference to the object.
Adam Crossland
@Chris B: This doesn't break the code, it fixes it. See my answer.
GreenMatt
Edited to remove 's', I should have realized you were using that in place of 'self'
KingRadical
All that does now is shadow the self outer class, which I need a reference to in the inner class.
Chris B.
+8  A: 

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.

FogleBird
Agreed, FogleBird.
Adam Crossland
+1  A: 

PyDev is telling you that Python class methods must have the 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'.

GreenMatt
+1 because consistency is key.
jathanism
You're misunderstanding what the code does. This is a context manager defined within another class method. So the inner class Subkey does, indeed, intend to call methods on the outer class Group's self variable. And does not require a reference to its own instance variable.
Chris B.
@Chris B.: That was not clear (at least to me) from the original question.
GreenMatt
+1  A: 

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

Albert Visser
+1  A: 

You can signal that you expected that to Pydev with a comment (#@NoSelf).

Using ctrl+1 in a line with some error from Pydev will always bring you a fix which will allow you to ignore the Pydev error in that line -- in that specific case, it'd add that #@NoSelf to that line, but it's also useful when some unused import is needed and other situations.

Fabio Zadrozny