Beginner Help - class problem or string copy semantics?

R Wentworth rhww at erols.com
Tue Apr 20 00:21:46 EDT 1999


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.




More information about the Python-list mailing list