Namespace weirdness

Paul Winkler slinkp23 at yahoo.com
Sun Jul 9 16:33:52 EDT 2000


Hi,

I think this is impossible... but is there a way for an object to
know what its parent namespace is?

I'm not sure if I phrased that correctly. Here's the simplest code I
can that demonstrates the idea.

class Food:
    def __init__(self, attr1):
        self.attr1 = attr1
    class BodyPart:
        def do_it(self):
            # This next line is a stand-in for what I want...
            print my_parent_namespace.attr1 


egg= Food("Hello from the egg!")
egg.head = egg.BodyPart()
egg.head.do_it() # I want this to print "Hello from the egg!"


-----------------------

I'm pretty new to OO design in general, so it could be that I'm nuts
and there's no good reason to ever want to do this...

As far as I can tell, it's impossible because instances don't "know
about" the names that reference them, since there may be an
unlimited number of such references. So there is no way for egg.head
to know about egg.attr1 unless I pass it as an argument to
egg.head.do_it() or egg.head.__init__()...

But I would love to be proved wrong since I want egg to have a LOT
of attributes, and I want it to contain a lot of objects that know
where to find them.

I have thought of one solution, which is to pass in the namespace as
an argument to the enclosed class, as part of its __init__(). That's
a pretty minimal amount of work to get the result I want.

class Food:
    def __init__(self, attr1):
        self.attr1 = attr1
    class BodyPart:
	def __init__(self, parent_namespace):
            self.parent_namespace = parent_namespace
        def do_it(self):
                # Now it should work.
                print self.parent_namespace.attr1 


egg = Food('Hello from the egg.')
egg.head = egg.BodyPart(egg)
egg.head.do_it()

------------------------

But for some reason this bothers me... maybe it's just that I know
what I mean and I wish python could read my mind. :)

Imagine this conversation:

me: "Bob, please say hello."

Bob: "Hello!"

me: "Bob, what is your name?"

Bob: "I have no idea."

me: "Then why did you answer when I called you Bob?"

(The answer, of course, is that Bob only does things that python
tells him to do, and it's python, not Bob, that initiates Bob's
actions; and when it does so, it doesn't refer to him by name, it
just speaks to him privately... at least in this weird analogy I'm
making.)

Maybe I'm off the deep end here, but I think it would be really
interesting if it were possible for an object to find out how it was
called. Sort of analogous to sys.argv[0]. And even more interesting
if spam.eggs.bacon had some way of knowing that it lives in eggs
which lives in bacon... 

.................    paul winkler    ..................
slinkP arts:   music, sound, illustration, design, etc.
        web page:  http://www.ulster.net/~abigoo        
      A member of ARMS:   http://www.reacharms.com
               or http://www.mp3.com/arms



More information about the Python-list mailing list