views:

182

answers:

3

Relatively new to Python, and I saw the following construct in the PyFacebook library (source here: http://github.com/sciyoshi/pyfacebook/blob/master/facebook/init.py#L660). I'm curious what this does because it appears to be a class that inherits from itself.

class AuthProxy(AuthProxy):
    """Special proxy for facebook.auth."""

    def getSession(self):
        """Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=auth.getSession"""
        ...
        return result

    def createToken(self):
        """Facebook API call. See http://developers.facebook.com/documentation.php?v=1.0&method=auth.createToken"""
        ...
        return token

what is this doing?

Tangentially related, I'm using PyDev in Eclipse and it's flagging this as an error. I'm guessing that's not the case. Anyway to let Eclipse know this is good?

+3  A: 

It's using the AuthProxy imported from a different module (check your imports) and deriving from it.

Ignacio Vazquez-Abrams
If that's the case (and it probably is) this is a *strong* argument for doing `import other_module` and then saying `class AuthProxy(other_module.AuthProxy)`. This prevents pollution of the local moudle's namespace and it highlights the fact that there are two things that *appear* to have the same name, yet are different things.
Peter Rowell
Another option is `import other_module.AuthProxy as OtherAuthProxy` along with `class AuthProxy(OtherAuthProxy)`.
Ignacio Vazquez-Abrams
Either solution would probably fix the PyDev error reporting issue, as well.
Devin Jeanpierre
Can you look at the source (linked in the original question) and explain where the other AuthProxy class is coming from? Or the FriendsProxy or PhotosProxy classes, which uses the same pattern?
Bialecki
+10  A: 

The class statement there doesn't make the class inherit from itself, it creates a class object with the current value of AuthProxy as a superclass, and then assigns the class object to the variable 'AuthProxy', presumably overwriting the previously assigned AuthProxy that it inherited from.

Essentially, it's about the same as x = f(x): x isn't the value of f on itself, there's no circular dependence-- there's just the old x, and the new x. The old AuthProxy, and the new AuthProxy.

Devin Jeanpierre
If that's the case, where is the original "AuthProxy" class coming from? There's no import for that class and while I might believe that AuthProxy is some generic class, the same pattern is used for a "FriendsProxy" and "PhotosProxy."
Bialecki
This is definitely confusing. Don't do it. It's definitely being imported somewhere; stick a "print AuthProxy.__module__" before this definition to see where it's from.
Glenn Maynard
+2  A: 

The "former" AuthProxy is created by __generate_proxies (it's not very nice code, there is even an exec and eval in it :)), but the author wanted also define some methods on top of it.

Messa
Awesome, thanks!
Bialecki
Wow, I think I just threw up a bit in my mouth...
Ignacio Vazquez-Abrams
It's like there's a party in my mouth, and everyone's throwing up.
Glenn Maynard