oop - optional arguments in initializer of python class -
I was thinking that someone quickly explains how to define user-defined objects efficiently and in a dragon manner with alternative arguments Prepare For example, I want to create this object:
square object: def __init __ (self, some_other_object, i, * j, * k): self.some_other_object = some_other_object self.i = I # If J is specified, then assume that it is = i if (j == none): self.j = i else: self.j = j # if k is given, value 0 if (k = = None): self.k = 0 and: self.k = k Is there a better way of doing this?
Edit: I have changed the code so that it is understood more comprehensive and more easily.
You can define the default parameters:
class optical transition (Object): def __init __ (self, chemical, i, j = none, k = 0) self.chemical = chemical self.i = i self.k = k self.j = j if j is not, no more No I If you do not explicitly call the class with j and k , they will use those default When you create an object, then you use all four parameters as usual. You can: optical transition ('sodium', 5, 100, 27) . If you want to use the default value for j but not for k , then you can also by defining the name of the parameter: optical Transition ('sodium', 5, k = 27) .
Comments
Post a Comment