Namespace weirdness
Gordon McMillan
gmcm at hypernet.com
Sun Jul 9 22:23:59 EDT 2000
Paul Winkler wrote:
>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
Python doesn't nest class definitions (or function defs) in a way that is
particularly interesting. Your code creates a class object which is
accsessible as Food.BodyPart, but BodyPart has no idea it was defined
inside Food. As far as it's concerned, it is toplevel.
But there is absolutely no cannibalism in Python. None at all.
>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...
OO means different things to different people. Some languages make a big
deal out of this kind of thing. Python doesn't.
When I say "none at all", I mean, of course, there's only a reasonable
level of these kinds of incidents....
-Gordon
More information about the Python-list
mailing list