[Tutor] Accessing the name of an instance variable
comber at cix.co.uk
Tue Nov 18 10:36:01 EST 2003
ah, yes. But the point of it all is to get the actual variable name.
This is so when the contents of the container is printed the variable name
as it appears in the program is also printed for debugging (or proof-reading
would be better description).
I think what I will do is write it all in a simple one-module script so the
variable names are available via globals(). That will do for proof-reading.
From: Goncalo Rodrigues [mailto:op73418 at mail.telepac.pt]
Sent: 18 November 2003 14:52
To: Edward Comber; tutor at python.org
Subject: Re: [Tutor] Accessing the name of an instance variable
On Tue, 18 Nov 2003 10:51:17 -0000, you wrote:
>To explain why I want to this sort of thing - I have derived a simple
>language for my partners to use in our medical practice by subclassing the
>Python container classes. They don't program themselves but can easily
>handle these classes which extract and manipulate groups of patients
>depending on diseases.
>A definition takes the form of..
>non_smoker_group = DiseaseCode(['1371','137L','137P0','137S','137T'])
>which extracts matching patients from the DB.
>I want them to be able to do some simple debugging by outputting the
>contents of the containers preceded by the container name, so they can
>that the codes they have used are correct (by doing a lookup in another
>I can do this of course by explicitly stating the container name:
>but it would be nicer and more fool-proof to incorporate it in container
Great but what does that got to do with messing with globals?
Let me go back a little and repost the example you posted a while
self.one = x
self.two = y
this_instance = id(self)
for name, instance in globals().items(): # vars()
if id(instance) == this_instance:
print 'Description of', name
The describe method, as you code it, is very bizarre. What you seem to
be doing is to go through the global names to *find the instance
itself* and then print a description. Don't do that, there is no need!
The first argument being called self is a clue... :-)
Bear with me a little. When you do the following
#Create an A instance.
a = A(1, 2)
The last line is equivalent to:
So that, as you can see, when the describe method is called the self
argument is set to the instance itself.
So describe can be coded just as
print "Description of", self
No need to grovel through the globals to find the instance, since the
instance is supplied as... self :-)
Just one more note: It is better for a method like describe to return
"something that can be printed" (a string) instead of print-ing
directly. That way you can combine describe with other methods,
compose fancier descriptions, etc.
return "Describing\n" + "Description of " + repr(self)
If you need more help just holler. With my best regards,
P.S: Consider reading the tutorial coming with every Python distro or
reading some of the online tutorials to learn more about objects, for
example, A. Gauld's is great. He is also on this list so you can
always try to squeeze more information out of him if my attempts at
explanation are too unnerving :-)
More information about the Tutor