[Tutor] Accessing the name of an instance variable

Gonçalo Rodrigues op73418 at mail.telepac.pt
Tue Nov 18 09:52:19 EST 2003

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 check
>that the codes they have used are correct (by doing a lookup in another DB).


>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
>behaviour like

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

class A:
    def __init__(self,x,y):
        self.one = x
        self.two = y

    def describe(self):
        print 'Describing'
        this_instance = id(self)
        for name, instance in globals().items(): # vars()
            print name
            if isinstance(instance,A):
                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)
#Describe it.

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

def describe(self):
     print "Describing"
     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.

def describe(self):
    return "Describing\n" + "Description of " + repr(self)

etc., etc.

If you need more help just holler. With my best regards,
G. Rodrigues

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 mailing list