I have a class Person
and 2 classes Child
and Adult
that inherit from `Person.
class Person:
def __init__(self, name):
self.name = name
self.age = 0
def say_hi(self):
print("Hi, I am " + self.name)
def set_age(self, age):
self.age = age
class Child(Person):
def play_with_children(self):
print("Playing with children")
class Adult(Person):
def drink_alcohol(self):
print("drinking")
I want to create an instance from Person
then set it's age
and according to it this instance should "become" a Child
or an Adult
.
Also if it's a Child
and it's age
increases I'd like it to "become" an Adult
.
Note that this code has an error because the Person
's constructor expects a name
because it's meant to create a new instance.
person1 = Person("Marvin")
person1.set_age(15)
if person1.age < 21:
person1 = Child()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Child(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.play_with_children()
person1.set_age(25)
if person1.age > 21:
person1 = Adult()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Adult(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.drink_alcohol()
Is this even possible?
Can this be done without creating new instances?
PS: this is just a sample code showing the problem from a more complex problem that I have (Not really children and adults :D ).