From: rhww at erols.com (R Wentworth)
Date: Tue, 20 Apr 1999 00:21:46 -0400
Subject: Beginner Help - class problem or string copy semantics?
References: <7fg8a0$3ib$1@nnrp1.dejanews.com>
Message-ID: <371C00DA.895423A6@erols.com>
X-UID: 487                                                  


cmedcoff at my-dejanews.com wrote:
> 
> As will soon be apparent I am totally new to Python. In the code fragment
> below I expect to see the output "foobar", but I do not.  Can anyone tell me
> why? 
...
> class Test:
>     _name = ""
>     def __init__(self, name):
>         _name = name
>     def show(self):
>         print self._name
> 
> mytest = Test("foobar")
> mytest.show()

The line "_name = name" should be "self._name = name".
Unlike C++, Python does not supply an implicit "this" or "self".
(Once you get used to it, this is actually a good thing.)
The "_name" you assigned to is simply a local variable within
the __init__() method; it vanishes as soon as the method ends.




