覆盖Python中的类变量
我试图了解一点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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
意味着
default
在定义函数时被求值,就像 Python 中通常的那样。因此,定义如下所示:defl 的值作为默认参数存储在函数对象上,并在调用不带参数的方法时使用。您可以查看诸如
print Descend.showDefaultValue.im_self.default
之类的函数的默认值来验证这一点。如果你想从当前类中获取默认值,那么你可以从那里获取它:
means that
default
gets evaluated when the function is defined, as usual in Python. So the definition looks like this then: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 likeprint 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:
类变量被覆盖。尝试一下
你的方法不起作用的原因更多地与Python处理函数默认参数的方式有关,而不是与类属性有关。
defl
的默认值是在 Python 定义showDefaultValue
的绑定时计算的,并且只执行一次。当您调用方法时,使用的默认值是定义时评估的值。在您的情况下,
defl
绑定到default
变量的值,就像在执行Base
的类主体形式期间一样。无论您稍后如何调用showDefaultValue
(即,通过Base
还是通过Descend
),该值在的所有后续调用中都保持固定>显示默认值
。The class variable is being overwritten. Try
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 forshowDefaultValue
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 thedefault
variable as it was during the execution of the class body form ofBase
. Regardless of how you callshowDefaultValue
later on (i.e., whether viaBase
or viaDescend
) that value remains fixed in all subsequent invocations ofshowDefaultValue
.