Python - Constructors and Object Initialization
What will this code print?
class Person:
def __init__(self, age=30):
self.age = age
p1 = Person()
p2 = Person(25)
print(p1.age, p2.age)What will this code print?
class Person:
def __init__(self, age=30):
self.age = age
p1 = Person()
p2 = Person(25)
print(p1.age, p2.age)p1 is created without argument, so age defaults to 30. p2 is created with 25.p1.age and p2.age outputs '30 25'.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions