Home > Net >  Update conditional variable in a class when original attribute variable is changed outside of the cl
Update conditional variable in a class when original attribute variable is changed outside of the cl

Time:06-17

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))

CodePudding user response:

I worked it out. I added another method to my code ( I called it birthday ) added a year to the age attribute, and then added the conditional statement

Hope this helps other newbies

def birthday(self):
    self.age  =1
    if self.age > 2:
        self.dogage = (((self.age-2)*5) 15 9)
    elif self.age == 2:
        self.dogage = 15 9
    else: 
        self.dogage = 15

CodePudding user response:

__init__ is called only once during the time of object creation. So, the value of dogage doesn't change when you change the value of age after object creation. To get the desired output, we can use setter and getter. So, I would suggest to use property decorator.

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.dogage = None
        self.age = age
    
    @property
    def age(self):
        return self._age
    
    @age.setter
    def age(self, age):
        if age > 2:
            self.dogage = ((age - 2) * 5)   24
        elif age == 2:
            self.dogage = 24
        else: 
            self.dogage = 15
        
        self._age = age

Output:

>>> dog = Dog('Python', 2)

>>> print(dog.age)
>>> 2
>>> print(dog.dogage)
>>> 24

>>> dog.age = 5
>>> print(dog.age)
>>> 5
>>> print(dog.dogage)
>>> 39
  • Related