class Disease:
def __init__(self, category, name, nicknames, sx, *labs, **inheritance):
self.category = category
self.name = name
self.nicknames = nicknames
self.sx = sx
self.labs = labs #incl. vitals
self.inheritance = inheritance
def printLabs(self):
for item in self.labs:
print(item)
This works fine, but when I try to initialize a Disease with three optional values, or with the optional value in the middle (ie nicknames), it doesn’t work.
Is there any way around these issues?
Advertisement
Answer
To make a value sometimes optional, you either have to pull it from *labs or **inheritance, or provide a default value such as:
class Disease: def __init__(self, category, name, nicknames=None, sx=None, *labs, **inheritance):
Now, if the user does not provide a value for nicknames, or sx, they remain as None.
Note: Optional (default) arguments like this must be at the end of the function declaration – meaning you cannot do something like def __init__(self, category, name=None, nicknames, sx, *labs, **inheritance) because after name, all arguments must have default values or else you’ll get a a SyntaxError.