So, I'm trying to get the object that a custom object is 'inside' of. Here's an example.
Assume that o is an object - it doesn't matter what kind, it can just store variables.
o = Object()
class Test():
def __init__(self):
self.parent = o ## This is where I fall off; I want to be able to access
## the o object from within the Test object
o.test = Test()
So, how would I get o from inside of Test? I know I could write the Test function to pass it in with
o.test = Test(o)
but I'd rather do it from inside the class definition.
Are you talking about inheritance? It's not very clear what you mean, but if you just want to do this:
It works perfectly.
**ed: This is not at all how you should do inheritance, if you want to do that. However, the way you describe your problem I think you want to create a tree-like structure of objects, not have classes inherit properties from each other.
If you want inheritable objects do it properly
(I know this is an old question but since it's unanswered...)
You could try messing with the garbage collector. Was going to suggest looking at the module level objects but some quick code experimentation gave this: Try the
gc.get_referrers(*objs)
function.The results aren't straightforward so you'll have to apply your own logic for how determine which
f
is the one you're looking for. Notice the'f': <__main__.Foo object at ...>
below:For bonus points, use
gc.get_referents(*objs)
on those to check if the sameb
is in their list.Some notes/caveats:
init
or another method. That's what I would do, instead of my suggestion above.you could do something like this:
well, I donno what exactly you are trying. but I have done something like this.
and to make a parent you can say
Hope this will help you.