I'm learning Python, and playing around with creating objects using a class. I have a simple dog class that contains a conditional statement that turns the dog's age into 'dog years' The problem is, if I change the dogs age once the object is instantiated it does not call the class, and therefore doesn't run through the conditional statement again. Should I keep all functions that might need updates outside of a class?
My code is
class Dog:
species = "Mammal" # class attribute - this attribute is set for all objects, a default value.
legs = 4
fur = True
tail = True
def __init__(self,name,age,toy,markings): # initializer that constructs a new oblect (instantiate)
self.name = name # instance attributes take the value of name passed in
self.age = age # and assigns them to the class to the variables self.name and self.age
self.toy = toy
self.markings = markings
if self.age > 2:
self.dogage = (((age-2)*5)+15+9)
elif self.age == 2:
self.dogage = 15+9
else:
self.dogage = 15
fido = Dog("Fido", 6, "Mr Squeeky","black spots, white fur")
Pluto = Dog("Pluto", 10, "Frisbee", "black with white spot forehead")
print("{} is {} years old and is {} years old in terms of dog years".format(fido.name, fido.age, fido.dogage))
print("{} is {} years old and is {} years old in terms of dog years".format(Pluto.name, Pluto.age, Pluto.dogage))
# one year later...
fido.age = 7 # updates the current object witht he new data (age of the dog)
Pluto.age = 11 # updates the current object witht he new data (age of the dog)
print("One Year Later...")
print("{} is {} years old and is {} years old in terms of dog years".format(fido.name, fido.age, fido.dogage))
print("{} is {} years old and is {} years old in terms of dog years".format(Pluto.name, Pluto.age, Pluto.dogage))