Which member is it?

David Goodger dgoodger at bigfoot.com
Thu Jun 29 23:33:03 EDT 2000


on 2000-06-29 18:30, Curtis Jensen (cjensen at bioeng.ucsd.edu) wrote:

> class Foo:
>   def __init__(self):
>     self.bar1 = [1,2,3]
>     self.bar1 = [1,2,3]
>     self.bar2 = [1,2]
>     self.bar2 = [1,2,3,4]
> 
> tmp = Foo()
> 
> If I make reference to tmp.bar1, which do I get or, does it matter?

The second one.

> If I make reference to tmp.bar2, which do I get?  it will make a
> difference here.

The second one.

>>> tmp.bar2
[1, 2, 3, 4]

> In my code, it seems tmp.bar1 doesn't seem to cause problems.  However,
> tmp.bar2 does cause some funny problems, especialy when making calls to
> C modules.  What exactly does python do when you reference same name
> with different bindings?  Thanks.

The second "self.bar2 = [...]" assignment simply binds the name "self.bar2"
to new object, the list [1,2,3,4]. The original binding is broken, and the
object [1,2] is gone (reclaimed by the interpreter). No magic!

What funny problems are you talking about? Perhaps you can post examples?

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list