1
Good afternoon, everyone
I created a very simple python class with a constructor __init__
. The constructor expects to receive 3 parameters, and the 3 have default value.
I would like to know how to instantiate the class passing only parameter 1 and 3.
If I charge this way it works :
p = Person('Wagner')
That’s how it works too
p = Person('Wagner','xxx')
But in this way error occurs:
p = Person('Wagner', ,27)
Error:
File "Person.py", line 11
p = Person('Wagner', ,27)
^
SyntaxError: invalid syntax
If I instantiate this way it also works
p = Person('Wagner','' ,27)
but supposing I had 10 parameters with values default and want to pass just the number 7,9,10 as I would ? I would be obliged to pass ''
empty, even if they have a default value in the constructor ?
class Person:
def __init__(self, name='not defined', last_name='not defined', age=0):
self.name = name
self.last_name = last_name
self.age = age
def printPerson(self):
print("Name: " + self.name + "\nLast Name: " +
self.last_name + "\nAge: " + str(self.age))