wasm-demo/demo/ermis-f/python_m/cur/0365

49 lines
1.5 KiB
Plaintext

From: wtanksle at dolphin.openprojects.net (William Tanksley)
Date: Mon, 19 Apr 1999 22:11:59 GMT
Subject: Beginner Help - class problem or string copy semantics?
References: <7fg8a0$3ib$1@nnrp1.dejanews.com>
Message-ID: <slrn7hnahf.kc.wtanksle@dolphin.openprojects.net>
Content-Length: 1216
X-UID: 365
On Mon, 19 Apr 1999 21:47:47 GMT, 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? All the bookstores seem to be out of "Learning Python". Are they out of
>print already or has the initial shipment still not released?
I suspect that the store hasn't ordered any. Keep bugging them :).
>class Test:
> _name = ""
> def __init__(self, name):
> _name = name
> def show(self):
> print self._name
First of all, I suspect that you're using the underscore because you want
the variable to be private. If so, try a double underscore, like __name.
>mytest = Test("foobar")
>mytest.show()
The problem is that you're setting "_name" (a variable local to the
__init__ function) instead of "self._name". Add that "self." and you'll
be fine.
Also, you don't need to have a class variable named the same as your
object variable -- it'll never get viewed. Feel free to remove the '_name
= ""' line from the class definition.
>Regards,
>Chuck
--
-William "Billy" Tanksley
"But you shall not escape my iambics."
-- Gaius Valerius Catullus