class Animal(): """ I have here , chosen member variable is_hungry.""" is_hungry = True def __init__(self, name, age): self.name = name self.age = age """ Here , creating an instance hippo.""" hippo = Animal("Gary", 5) print( hippo.is_hungry) # prints True # and now I'll change hippo.is_hungry = False hippo.is_hungry = False print(hippo.is_hungry) # prints False #And here if , I print Animal.is_hungry , I get True print(Animal.is_hungry)
Would'nt it be just better to have def init(self, name, age, is_hungry=True) and then change it for each instance separately, if required rather than generating an member variable. Since, Animal.is_hungry will always show True even if we have had many test cases before
I am not able to understand the use of the member/class variable.