trying to grasp OO : newbie Q?

M

mitsura

Hi,

I just started with Python and I am new to OO programming.
Here is a simple code:
"
class Obj:
myVar = 1

def __init__(self):
myVar = 2

#


myObj = Obj()

print myObj.myVar
"

The output is of this script is '1'. I would except it to be '2'.
I not understanding something fundamentally here.

Can anybody explain?

With kind regards,

Kris
 
A

Azolex

Hi,

I just started with Python and I am new to OO programming.
Here is a simple code:
"
class Obj:
myVar = 1

def __init__(self):
myVar = 2

#


myObj = Obj()

print myObj.myVar
"

The output is of this script is '1'. I would except it to be '2'.
I not understanding something fundamentally here.

You want to replace "myVar = 2" by "self.myVar = 2"
to get the result you expect.
Can anybody explain?

Your "myVar = 1" sets a variable in the namespace of the class

Your "myVar = 2" sets a variable in the local frame of execution of the
__init__ method, that is invisible outside that frame

Putting "self.myVar = 2" will set a variable in the instance dict that
will shadow the class variable of the same name when you access myObj.myVar

hth
 
J

J Rice

Someone should correct me if I'm wrong but:

If you add "print myVar" to __init__, you will see that myVar is
assigned to "2" in that function. It doesn't change the assignment of
"1" in the class because myVar in __init__ is local to __init__. If
you want to change myVar for the whole class, you need to reference it
as self.myVar.

If you change your __init__ assignment to self.myVar, you will get the
"2" output you expect. Also note that if you make a new variable in
__init__ (myVar2)and try to get its value with myObj.myVar2, you will
get an error that the variable doesn't exist. But if you create the
variable using self.myVar2, assigning it to the class, you will be able
to access it.

Check some of the references on variable scope.
 
B

Ben C

Hi,

I just started with Python and I am new to OO programming.
Here is a simple code:
"
class Obj:
myVar = 1

def __init__(self):
myVar = 2

#


myObj = Obj()

print myObj.myVar
"

The output is of this script is '1'. I would except it to be '2'.
I not understanding something fundamentally here.

Good example, it demonstrates these things quite well.

My understanding of this is as follows:

myVar is a class variable, because you define it in the body of the
class statement (not in any method).

In Python, classes and instances can both be thought of as dictionaries,
i.e. sets of key-value pairs.

In this example, Obj is a class and myObj is an instance.

When you write print myObj.myVar, the interpreter looks first in myObj
for a value matching the key "myVar". It doesn't find one there (I
explain why not below). So it continues the search in myObj's class,
which is the class Obj. It does find one there, with the value 1, so
that's what you get.

The myVar that you define in __init__ is not an instance variable, but a
local variable (local to the function __init__). To make an instance
variable you have to use "self" explicitly:

def __init__(self):
self.myVar = 2

If you write it like this, your program will print out 2.

People coming from languages like C++ where this-> is implicit sometimes
forget to write "self."; in Python you always need it to get at the
instance.
 
W

Wildemar Wildenburger

J said:
Someone should correct me if I'm wrong but:
If
you want to change myVar for the whole class, you need to reference it
as self.myVar.
wrong: If you want to change myVar for the whole *class*, you need to
reference it as Obj.myVar (prefix with classname).
self.myVar will change it for that one instance only.

other than that, agreed :)
wildemar
 
M

mitsura

Thanks guys,

It is starting to make much more sense. Most documentation I find about
OO is very academic
 
J

John Salerno

Thanks guys,

It is starting to make much more sense. Most documentation I find about
OO is very academic

Couldn't we also say that this issue of namespace scope is a little more
specific to Python than OOP in general? I could very easily be wrong,
but I wouldn't want the poster to think that this is how OOP works always.
 
K

Kent Johnson

John said:
Couldn't we also say that this issue of namespace scope is a little more
specific to Python than OOP in general? I could very easily be wrong,
but I wouldn't want the poster to think that this is how OOP works always.

No, the confusion in the OP was between class attributes, instance
attributes and local variables. This distinction is pretty fundamental
to OOP.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,183
Latest member
GarfieldBa

Latest Threads

Top