Overriding class variables in python

后端 未结 2 1787
滥情空心
滥情空心 2021-02-19 05:09

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 Bas         


        
2条回答
  •  不知归路
    2021-02-19 05:40

    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)
    

提交回复
热议问题