[EMAIL PROTECTED] wrote:
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to