覆盖Python中的类变量

发布于 2024-10-12 23:54:12 字数 621 浏览 6 评论 0原文

我试图了解一点Python(2.6)如何处理类、实例等,在某个时刻,我尝试了这段代码:

#/usr/bin/python2.6

class Base(object):
    default = "default value in base"

    def __init__(self):
        super(Base, self).__init__()

    @classmethod
    def showDefaultValue(cls, defl = default):
        print "defl == %s" % (defl)


class Descend(Base):
    default = "default value in descend"

    def __init__(self):
        super(Descend, self).__init__()

if __name__ == "__main__":
    Descend.showDefaultValue()

输出是:“base中的默认值”

我想知道为什么“默认值” “字段没有被下降类覆盖...有什么方法可以覆盖它吗?为什么它没有被覆盖?

任何提示(或解释页面的链接将不胜感激)。谢谢你!

I'm trying to understand a bit how Python (2.6) deals with class, instances and so on, and at a certain point, I tried this code:

#/usr/bin/python2.6

class Base(object):
    default = "default value in base"

    def __init__(self):
        super(Base, self).__init__()

    @classmethod
    def showDefaultValue(cls, defl = default):
        print "defl == %s" % (defl)


class Descend(Base):
    default = "default value in descend"

    def __init__(self):
        super(Descend, self).__init__()

if __name__ == "__main__":
    Descend.showDefaultValue()

The output is: "default value in base"

I was wondering why the "default" field is not overwirtten by the Descend class... Is there any way to overwrite it? Why isn't it being overwritten?

Any hint (or link to an explanatory page will be appreciated). Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2024-10-19 23:54:12
def showDefaultValue(cls, defl=default):

意味着 default 在定义函数时被求值,就像 Python 中通常的那样。因此,定义如下所示:

def showDefaultValue(cls, defl="default value in base"):

defl 的值作为默认参数存储在函数对象上,并在调用不带参数的方法时使用。您可以查看诸如 print Descend.showDefaultValue.im_self.default 之类的函数的默认值来验证这一点。

如果你想从当前类中获取默认值,那么你可以从那里获取它:

@classmethod
def showDefaultValue(cls, defl=None):
    if defl is None:
        defl = cls.default
    print "defl == %s" % (defl)
def showDefaultValue(cls, defl=default):

means that default gets evaluated when the function is defined, as usual in Python. So the definition looks like this then:

def showDefaultValue(cls, defl="default value in base"):

This value of defl is stored as a default argument on the function object and used when you call the method without arguments. You can look at the defaults of a function like print Descend.showDefaultValue.im_self.default to validate this.

If you want to get the default from the current class then you have get it from there:

@classmethod
def showDefaultValue(cls, defl=None):
    if defl is None:
        defl = cls.default
    print "defl == %s" % (defl)
你げ笑在眉眼 2024-10-19 23:54:12

类变量被覆盖。尝试一下

@classmethod
def showDefaultValue(cls):
    print "defl == %s" % (cls.default,)

你的方法不起作用的原因更多地与Python处理函数默认参数的方式有关,而不是与类属性有关。 defl 的默认值是在 Python 定义 showDefaultValue 的绑定时计算的,并且只执行一次。当您调用方法时,使用的默认值是定义时评估的值。

在您的情况下,defl 绑定到 default 变量的值,就像在执行 Base 的类主体形式期间一样。无论您稍后如何调用 showDefaultValue(即,通过 Base 还是通过 Descend),该值在 的所有后续调用中都保持固定>显示默认值

The class variable is being overwritten. Try

@classmethod
def showDefaultValue(cls):
    print "defl == %s" % (cls.default,)

The reason your way doesn't work has more to do with the way Python treats default arguments to functions than with class attributes. The default value for defl is evaluated at the time Python defines the binding for showDefaultValue and this is done exactly once. When you call your method the default value used is what was evaluated at the time of definition.

In your case, defl was bound to the value of the default variable as it was during the execution of the class body form of Base. Regardless of how you call showDefaultValue later on (i.e., whether via Base or via Descend) that value remains fixed in all subsequent invocations of showDefaultValue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文