Confused yet again: Very Newbie Question

Matt Nordhoff mnordhoff at
Mon Jul 7 14:07:19 CEST 2008

mcl wrote:
> Why can I not the change the value of a variable in another class,
> when I have passed it via a parameter list.
> I am sure I am being stupid, but I thought passed objects were Read/
> Write

In Python, there are names which are bound to objects. Doing "foo = bar"
and then "foo = spam" (re)binds the name "foo" to the same object as
"spam" is bound to. This doesn't have any effect on any other names that
were bound to the same object as "bar".

> eg
> ------------------------------------------------------------
> #!/usr/bin/python
> class one():  #my Global Vars
>     fred = 'fred'
> class three():
>     def createJoe(self, myName):

Here, the local name "myName" is bound to the same string object as

>         c1 = one()
>         myName = 'Joe'  #********************* Question why does this
> not change variable fred in 'class one'

Here, you rebind the local name "myName" to the string object 'Joe'.
This doesn't change what one.fred is bound to.

>         print 'Three(Local): ' + myName + '    Three(Global): ' +
> c1.fred
> def main():
>     c1 = one()
>     c3 =three()
>     c3.createJoe(c1.fred)
> if __name__ == '__main__' : main()
> Results:
> Three(Local): Joe    Three(Global): fred
> 'fred' in 'class one' does not get changed to 'joe' in 'class three'
> 'createJoe', even though I have passed 'class one' 'fred' to
> 'createJoe'
> I hope this makes sense.
> I did not think you had to make the distinction between 'byvar' and
> 'byref' as in Basic.
> Thanks
> Richard

There are a couple good articles about this online. The one below is
lengthy and has ASCII art, but I don't remember if I liked it.


BTW, in this example, there doesn't seem to be any need for you to be
using classes. Also, you should always use new-style classes unless you
have a specific reason not to

>>> class MyClass(object):

More information about the Python-list mailing list