assigning class variable as default value to class method argument

前端 未结 4 1769
名媛妹妹
名媛妹妹 2020-12-01 07:00

I would like to build a method inside a class with default values arguments taken from this class. In general I do filtering on some data. Inside my class I have a method wh

4条回答
  •  情深已故
    2020-12-01 07:43

    Default arguments get evaluated only once, when the definition is executed. Instead, do this:

    def doSomething(self, a=None):
        if a is None:
            a = self.z
        self.z = 3
        self.b = a
    

    See also http://docs.python.org/release/3.3.0/tutorial/controlflow.html#more-on-defining-functions.

提交回复
热议问题