tags:

views:

1139

answers:

4

How do these 2 classes differ?

class A():
    x=3

class B():
    def __init__(self):
        self.x=3

Is there any significant difference?

+34  A: 

A.x is a class variable. B's self.x is a instance variable.

i.e. A's x is shared between instances.

It would be easier to demonstrate the difference with something that can be modified like a list:

#!/usr/bin/env python

class A:
    x = []

    def add(self):
        self.x.append(1)


class B:
    def __init__(self):
        self.x = []

    def add(self):
        self.x.append(1)


x = A()
y = A()
x.add()
y.add()
print "A's x:",x.x

x = B()
y = B()
x.add()
y.add()
print "B's x:",x.x

Output

A's x: [1, 1]
B's x: [1]

Douglas Leeder
Maybe also post the output of your script, then one can see the difference without copying and running it oneself...
Martin
I added the output.
Unkwntech
Is python's self equivalent to Java's this then? Excuse the noobishness please
Jean Azzopardi
@Unkwntech - thanks.
Douglas Leeder
@Jean - Yes-ish - self has to be just the conventional name given to the first parameter of instance methods - and python explicitly passes the current instance of instance methods as the first argument to instance methods. But it does the same job as Java's this
Douglas Leeder
@Jean Azzopardi: self is almost like Java (and c++) this. Self is simply required; this is sometimes reasoned out by the Java compiler (other times it's required.)
S.Lott
@Douglas Leeder - no problem.
Unkwntech
thanks to all who answered my question.
Jean Azzopardi
Just a quick note: You can reassign the variable to decouple it from the class variable. I.e. change add() to do "self.x=self.x + [1]".
Deestan
+6  A: 

A.x is a class variable, and will be shared across all instances of A, unless specifically overridden within an instance. B.x is an instance variable, and each instance of B has its own version of it.

I hope the following Python example can clarify:


    >>> class Foo():
    ...     i = 3
    ...     def bar(self):
    ...             print 'Foo.i is', Foo.i
    ...             print 'self.i is', self.i
    ... 
    >>> f = Foo() # Create an instance of the Foo class
    >>> f.bar()
    Foo.i is 3
    self.i is 3
    >>> Foo.i = 5 # Change the global value of Foo.i over all instances
    >>> f.bar()
    Foo.i is 5
    self.i is 5
    >>> f.i = 3 # Override this instance's definition of i
    >>> f.bar()
    Foo.i is 5
    self.i is 3
TerrorBite
+7  A: 

Just as a side note: self is actually just a randomly chosen word, that everyone uses, but you could also use this, foo, or myself or anything else you want, it's just the first parameter of every non static method for a class. This means that the word self is not a language construct but just a name:

>>> class A:
...     def __init__(s):
...        s.bla = 2
... 
>>> 
>>> a = A()
>>> a.bla
2
André
A: 

There have been lately some interesting posts regarding the use of self between Bruce Eckel and GvR. Check it out: this vs. this

hyperboreean