trying to grasp OO : newbie Q?

J Rice rice.jeffrey at gmail.com
Thu Apr 13 10:35:34 EDT 2006


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.




More information about the Python-list mailing list