8

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 name that created the self.x instance? In other words, if I was given the instance self.x, how do I get the name 'Class1'? Using self.x.__class__.__name__ will obviously only give you the Class2 name. Is this even possible? Thanks.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Chuck
  • 207
  • 1
  • 5
  • 11
  • 1
    `x` is a local variable, how are you "getting" it? – SilentGhost Oct 20 '09 at 10:14
  • 1
    In your example `x` is a local variable that only exists temporarily when `meow` is created. And `x` **is** an instance of `Class2`. What are you asking? Please fix your question so that it makes sense. – S.Lott Oct 20 '09 at 10:21
  • This was simply an example used to try to describe if there was a function for what I was trying to do; not actual code in use. I am able to retrieve the instances using callbacks. However, none of this is really relevant to the question. – Chuck Oct 20 '09 at 10:28
  • 1
    -1: The code is not relevant to the question. Please post actual code that's actually relevant to your actual question. If you can't post code that's relevant to the question, we can't help, can we? – S.Lott Oct 20 '09 at 12:20
  • 3
    Let me be clear, it being x and self.x is irrelevant. It was a simple question on capability using the structure as an example. It has been answered, the others knew what I was referring to. Yes, properly it would be self.x, yes I made a typo while making a quick EXAMPLE to explain. To me you're just splitting hairs at this point, however I'll edit the post since it's a big deal to you. Thanks for the vote down; real classy – Chuck Oct 20 '09 at 12:47
  • 3
    @Chuck: N00bs read this site for help. If your question is clear, then they will benefit from your question. If your question is unclear, then the site is cluttered and they will not benefit from what you've posted. The choice -- to be helpful to others -- is yours. – S.Lott Oct 20 '09 at 13:08

3 Answers3

6

You can't, unless you pass an instance of the 'creator' to the Class2() constructor. e.g.

class Class1(object):
    def __init__(self, *args, **kw):
        self.x = Class2("Woo!", self)

class Class2(object):
    def __init__(self, word, creator, *args, **kw):
        self._creator = creator
        print word

This creates an inverse link between the classes for you

workmad3
  • 25,101
  • 4
  • 35
  • 56
  • Yeah I had a feeling this was the case, either that or setting an attribute. Thanks, this is the answer I was looking for. – Chuck Oct 20 '09 at 10:29
  • @Chuck: well, he *is* setting an attribute, so either I didn't understand your comment or you didn't understand the answer :P – MestreLion Apr 26 '12 at 09:11
1

Set a variable on the class in question in your __init__() method that you then retrieve later on.

You'll get better answers if you ask better questions. This one is pretty unclear.

Paul McMillan
  • 19,693
  • 9
  • 57
  • 71
0

Your question is very similar to answered here. Note, that you can determine who created the instance in its constructor, but not afterwards. Anyway, the best way is to pass creator into constructor explicitly.

Community
  • 1
  • 1
Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • Thanks, that helps. Yeah was just wondering if I was missing any other way to do it; other than passing it on in some form. – Chuck Oct 20 '09 at 10:40