Get class from instance?

vhx

Golden Member
Jul 19, 2006
1,151
0
0
I am wondering if there is a way to get whatever class created the instance.

Example:
class Class1:
def __init__(self):
self.x = Class2('Woo!')

class Class2:
def __init__(self, word):
print word

meow = Class1()
How do I derive the class instance (or name) that created the x instance? In other words, if I was given the instance x, how do I get the name 'class1'. Using x.__class__.__name__ will obviously only give you the Class2 name. Is this even possible? Thanks.

EDIT: Most likely I'll have to pass self on. EX: self.x = Class2('Woo!', self).

Was just wondering if there was another way.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
I'm not entirely sure, but that sounds like something you'd have to keep track of yourself. I don't see any reason why an object would know who instantiated it without explicitly tracking that information.