继承自己的Python类?这个怎么用?

时间:2022-11-07 16:58:45

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.

相对较新的Python,我在PyFacebook库中看到了以下构造(来源:http://github.com/sciyoshi/pyfacebook/blob/master/facebook/init.py#L660)。我很好奇这是做什么的,因为它似乎是一个继承自己的类。

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?

切向相关,我在Eclipse中使用PyDev,并将其标记为错误。我猜不是这样的。无论如何让Eclipse知道这很好吗?

4 个解决方案

#1


11  

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.

那里的类​​声明没有使类继承自己,它创建一个类对象,当前值为AuthProxy作为超类,然后将类对象分配给变量'AuthProxy',可能会覆盖以前分配的AuthProxy它继承自。

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.

本质上,它与x = f(x)大致相同:x不是f本身的值,没有循环依赖 - 只有旧的x和新的x。旧的AuthProxy和新的AuthProxy。

#2


3  

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

它使用从不同模块导入的AuthProxy(检查导入)并从中派生。

#3


3  

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.

“前”AuthProxy是由__generate_proxies创建的(它不是很好的代码,甚至还有一个exec和eval :)),但是作者还希望在它上面定义一些方法。

#4


1  

To make Eclipse stop whining about it, do this:

要让Eclipse停止抱怨它,请执行以下操作:

class AuthProxy(AuthProxy): #@UndefinedVariable

class AuthProxy(AuthProxy):#@ UndefinedVariable

#1


11  

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.

那里的类​​声明没有使类继承自己,它创建一个类对象,当前值为AuthProxy作为超类,然后将类对象分配给变量'AuthProxy',可能会覆盖以前分配的AuthProxy它继承自。

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.

本质上,它与x = f(x)大致相同:x不是f本身的值,没有循环依赖 - 只有旧的x和新的x。旧的AuthProxy和新的AuthProxy。

#2


3  

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

它使用从不同模块导入的AuthProxy(检查导入)并从中派生。

#3


3  

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.

“前”AuthProxy是由__generate_proxies创建的(它不是很好的代码,甚至还有一个exec和eval :)),但是作者还希望在它上面定义一些方法。

#4


1  

To make Eclipse stop whining about it, do this:

要让Eclipse停止抱怨它,请执行以下操作:

class AuthProxy(AuthProxy): #@UndefinedVariable

class AuthProxy(AuthProxy):#@ UndefinedVariable