I have a class function that takes 4 arguments, all class member variables in practice. Usually only one of these arguments is modified at a time and the rest of the time the would-be defaults (current values of the class member variables) are passed to the function/written out even though they aren't changing.
I thought the best would be to make these class member variables the defaults, but I'm having a hard time getting the best syntax. The following code produces behavior I desire - using a class member variable as a default parameter to a function. But it's ugly:
class test:
def __init__(self, a, b):
self.a = a
self.b = b
def testfunc(self, param = None):
if not param:
param = self.a
print(param)
if __name__ == '__main__':
a1 = test(1, 3)
a1.testfunc()
a2 = test(5, 5)
a2.testfunc()
#(prints 1 and 5 when called)
I would like to use the following:
def testfunc(self, param = self.a):
print(param)
But this gives the error:
NameError: name 'self' is not defined
What's a better way to do this?