Say I initialize the following class and attempt to call a method like so:
class tester():
def __init__(self, test):
self.test=test
def caller(self, test=test):
return test
t=tester(6)
t.caller()
#error
I'm hoping that the default arg for the parameter 'test' in the method 'caller' will be the self.test of the instance. Instead I get an error that 'test' is not defined. If I replace test=test with test=self.test I get an error that 'self' is not defined.
Setting the variable directly for the class as follows doesn't result in an error, but of course just simply returns the 'test' from the class, not from the instance:
class tester():
test=1
def __init__(self, test):
self.test=test
def caller(self, test=test):
return test
t=tester(6)
t.caller()
#returns 1
How do I specifically use the instance self.test as a default arg for the method? The reason I want to do this is because I'll be creating a lot of functions that will share many identical parameters that are consistent with each instance of the class. Rather than passing these arguments directly, the method parameters can default to the default values of the instance.
The reason I don't want to simply access the instance variables inside caller directly, but rather rely on a default method argument, is because sometimes I would like to have the option of calling these functions with different values.