I need to define a class that will take multiple keyword parameters.
My code looks like this:
class test1:
def __init__(self,parm1=val1, parm2=val2):
self.x = parm1
self.y = parm2
print('parm1',self.x)
def main():
x = test1(1,2)
but I get an error that val1 is not defined. What would be the proper way ?
You need to define
val1andval2before you define your classtest:This approach can have its advantages (say, if
val1andval2should be some generic default values read from the environment…), but is not really readable. It’d be best to use something like:where once again,
val1andval2are defined before the definition oftest…