Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between declaring data attributes inside or outside __init__ [duplicate]

Possible Duplicate:
Python: Difference between class and instance attributes

I'm trying to get my head around OOP in Python and I'm a bit confused when it comes to declare variables within a class. Should I declare them inside of the __init__ procedure or outside it? What's the difference?

The following code works just fine:

# Declaring variables within __init__
class MyClass:
    def __init__(self):
        country = ""
        city = ""
    def information(self):
        print "Hi! I'm from %s, (%s)"%(self.city,self.country)

me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()

But declaring the variables outside of the __init__ procedure also works:

# Declaring variables outside of __init__
class MyClass:
    country = ""
    city = ""
    def information(self):
        print "Hi! I'm from %s, (%s)"%(self.city,self.country)

me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()
like image 299
David Martinez Avatar asked Dec 12 '12 17:12

David Martinez


2 Answers

You're two versions of the code are very different. In python, you have 2 distinct entities: classes and class instances. An instance is what is created when you do:

new_instance = my_class()

You can bind attributes to an instance within __init__ via self (self is the new instance).

class MyClass(object):
    def __init__(self):
        self.country = ""  #every instance will have a `country` attribute initialized to ""

There's nothing terribly special about self and __init__. self is the customary name that is used to represent the instance that gets passed to every method (by default).

a.method()  #-> Inside the class where `method` is defined, `a` gets passed in as `self`

The only thing special here is that __init__ gets called when the class is constructed:

a = MyClass()  #implicitly calls `__init__`

You can also bind attributes to the class (putting it outside __init__):

class MyClass(object):
    country = ""  #This attribute is a class attribute.

At any point, you can bind a new attribute to an instance simply by:

my_instance = MyClass()
my_instance.attribute = something

Or a new attribute to a class via:

MyClass.attribute = something

Now it gets interesting. If an instance doesn't have a requested attribute, then python looks at the class for the attribute and returns it (if it is there). So, class attributes are a way for all instances of a class to share a piece of data.

Consider:

def MyClass(object):
    cls_attr = []
    def __init__(self):
        self.inst_attr = []

a = MyClass()
a.inst_attr.append('a added this')
a.cls_attr.append('a added this to class')
b = MyClass()
print (b.inst_attr) # []  <- empty list, changes to `a` don't affect this.
print (b.cls_attr) # ['a added this to class'] <- Stuff added by `a`!
print (a.inst_attr) #['a added this']
like image 37
mgilson Avatar answered Sep 23 '22 22:09

mgilson


In your first example you are defining instance attributes. In the second, class attributes.

Class attributes are shared between all instances of that class, where as instance attributes are "owned" by that particular instance.

Difference by example

To understand the differences let's use an example.

We'll define a class with instance attributes:

class MyClassOne:
    def __init__(self):
        self.country = "Spain"
        self.city = "Barcelona"
        self.things = []

And one with class attributes:

class MyClassTwo:
    country = "Spain"
    city = "Barcelona"
    things = []

And a function that prints out information about one of these objects:

def information(obj):
    print "I'm from {0}, ({1}). I own: {2}".format(
                obj.city, obj.country, ','.join(obj.things))

Let's create 2 MyClassOne objects and change one to be Milan, and give Milan "something":

foo1 = MyClassOne()
bar1 = MyClassOne()

foo1.city = "Milan"
foo1.country = "Italy"
foo1.things.append("Something")

When we call information() on the foo1 and bar1 we get the values you'd expect:

>>> information(foo1)
I'm from Milan, (Italy). I own: Something

>>> information(bar1)
I'm from Barcelona, (Spain). I own: 

However, if we were to do exactly the same thing, but using instances of MyClassTwo you'll see that the class attributes are shared between instances.

foo2 = MyClassTwo()
bar2 = MyClassTwo()

foo2.city = "Milan"
foo2.country = "Italy"
foo2.things.append("Something")

And then call information()...

>>> information(foo2)
I'm from Milan, (Italy). I own: Something
>>> information(bar2)
I'm from Barcelona, (Spain). I own: Something

So as you can see - things is being shared between the instances. things is a reference to a list that each instance has access to. So if you append to things from any instance that same list will be seen by all other instances.

The reason you don't see this behaviour in the string variables is because you are actually assigning a new variable to an instance. In this case that reference is "owned" by the instance and not shared at the class level. To illustrate let's assign a new list to things for bar2:

bar2.things = []

This results in:

>>> information(foo2)
I'm from Milan, (Italy). I own: Something
>>> information(bar2)
I'm from Barcelona, (Spain). I own: 
like image 200
Alex Couper Avatar answered Sep 23 '22 22:09

Alex Couper