assigning class variable as default value to class method argument

Your understanding is wrong. self is itself a parameter to that function definition, so there’s no way it can be in scope at that point. It’s only in scope within the function itself.

The answer is simply to default the argument to None, and then check for that inside the method:

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

Leave a Comment