1
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')
hippo.description()
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?

Cheers

masiewpao
  • 309
  • 3
  • 12
  • 1
    It would help to use Python nomenclature; `ocelot.health` is a *class attribute*, shared by all `Animal`s, whereas `ocelot.name` is an *instance attribute*, unique to that specific instance. Both can be accessed via `self` within *instance methods*. – jonrsharpe Jul 31 '15 at 06:43
  • See: http://stackoverflow.com/questions/7548546/python-class-attributes-and-instance-attributes and http://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes I think I should flag this as duplicate, but I have to idea of which of those :P – Filip Malczak Jul 31 '15 at 06:51
  • @FilipMalczak On the other hand the headline asks for the difference between method and variable, not class and instance attributes. – skyking Jul 31 '15 at 07:02
  • That's also why I haven't flagged this - it may be a duplicate but I'm not sure and it's to fuzzy to tell. Still, those links may be helpful. – Filip Malczak Jul 31 '15 at 07:04

2 Answers2

1

I assume you're coming from a more traditional OOP programming language like C++ or Java.

health in the Animal class is what you would refer to as a static member variable, but in Python this is called a class attribute because it is unique to the class.

name in the Animal class is what you would refer to as a member or instance variable, and in Python this is called an instance attribute because it is unique to each instance of a class.

You use self to refer to attributes within its own class.

  • Also note that the use of "`self`" is just a convention. You could use "`this`" if you really wanted to (but don't!) – juanchopanza Jul 31 '15 at 06:59
  • To be honest I am brand new to any sort of programming. I only tagged OOP because in the book which I am using it makes a mention that this is the basic idea surrounding it. Does this mean that in python it is wrong to think of an attribute as a variable? – masiewpao Jul 31 '15 at 07:06
0

First of all the difference between class and instance attributes are answered elsewhere.

The difference between a method and member variables are that while they are both attributes, a method is a function while a member variable is not (or need not be). Also a method is normally a class attribute (at least if you use new style classes).

However in python functions are first class objects so this may confuse a little more: it's perfectly valid to assign a member variable with a function (or vice versa), but then that will become somewhat different because normally a method is shared among all objects, but when assigned to an instance it becomes private to that instance.

self.foo may be used to access both instance attributes or class attributes (if instance attribute does not exist).

skyking
  • 13,817
  • 1
  • 35
  • 57