Consider the following code, I want you to explain why the code doesn't work because I have seen others to assign the class attributes outside __init__ and
call the attributes in one method from another, what is wrong with mine. The following three prints all result error saying the class has no attributes whatsoever.
class Person(object):
def __init__(self, age, height):
self.age = age
self.height = height
def weight_kg(self):
self.weight = 60
def ratio_kg2height(self):
self.ratio = self.weight / self.height
return self.ratio
mike = Person(23, 170)
print mike.weight
print mike.ratio_kg2height()
print mike.ratio
mike.weightisn't initialised until you callweight_kg. You'd need to make it a property.weightis going to be defined?