Difference between methods and member variables?

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health = 'good'
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age

hippo = Animal('Tom', '20')
sloth = Animal('Randy', '18')
ocelot = Animal('Birdman','57')
print ocelot.health
print hippo.health
print sloth.health

The code above is from codecademy’s python course. I am getting confused about some of the definitions surrounding OOP. If my understanding is correct, a function defined within a class is known as a method, which is why when it’s called, for example like this: ‘hippo.description()’, the ‘()’ are necessary because of the syntax involving functions.

However, I start to get confused with ‘self.name’ and ‘self.age’. Are these also methods? I was wondering if they were perhaps member variables, but then wouldn’t they be defined in the same way the variable ‘health’ was above? And if they aren’t member variables, how come they can be accessed using dot notation in the same way as the member variables?


Source: oop

Leave a Reply