views:

68

answers:

4

Before I ask this, do note: I want this for debugging purposes. I know that this is going to be some bad black magic, but I want to use it just during debugging so I could identify my objects more easily.

It's like this. I have some object from class A that creates a few B instances as attributes:

class A(object):
    def __init__(self)
        self.vanilla_b = B()
        self.chocolate_b = B()

class B(object):
    def __init__(self):
        # ...

What I want is that in B.__init__, it will figure out the "vanilla_b" or whatever attribute name it was given, and then put that as the .name attribute to this specific B.

Then in debugging when I see some B object floating around, I could know which one it is.

Is there any way to do this?

A: 

This is probably not the answer you are looking for but perhaps you could do something along this:

class A(object):
    def __init__(self):
        attrs = ('vanilla_b', 'chocolate_b')

        for attr in attrs:
            instance = B()
            setattr(self, attr, instance)
            instance.name = attr

Try wrapping that behavior to some superclass and you might be good to go.

bebraw
A bit problematic, I want to have different parameters in the creation of each `B`. If there won't be good enough magic, I might take this method.
cool-RR
Ok. To work around that you could instantiate attributes as a dict (key=name, value=object).
bebraw
A: 

If you have control over code then it is better to do it simply then rely on some black magic e.g. do

class A(object):
    def __init__(self)
        self.vanilla_b = B(name="vanilla_b")
        self.chocolate_b = B(name="chocolate_b")

otherwise you can use inspect module to go thru locals and self in prev frame and do some vodoo.

Anurag Uniyal
I'm aware of this method, thanks. My whole question is, "how do I do the voodoo?"
cool-RR
+2  A: 

You can use sys._getframe to get the line number where B() is called, then you can use inspect.getsourcelines to get the actual line of code. From there you can parse the line of code to get the thing to which B() is being assigned:

import sys
import inspect

class A(object):
    def __init__(self):
        self.vanilla_b = B()
        self.chocolate_b = B()

class B(object):
    def __init__(self):
        line_num = sys._getframe().f_back.f_lineno
        lines = inspect.getsourcelines( sys.modules[__name__] )[0]
        line = lines[line_num - 1]
        attr = line.split("=")[0].split(".")[1].strip()
        print "B() is being assigned to", attr

A()

If you put the above code into a python script and run it, then it will print

B() is being assigned to vanilla_b
B() is being assigned to chocolate_b

However, this won't work at the Python command prompt, since __main__ is a builtin module, and thus the inspect module can't retrieve its source lines. So you might want to wrap this in a try/catch block or something just in case your code ever gets called from any builtin module.

For the record, this is probably a bad idea, but you say that you're aware that it's not good practice and you're only doing it for debugging, so hopefully you'll be able to use this kind of trickery wisely.

Eli Courtwright
Ah, tying the file name to the code would be too annoying.
cool-RR
@cool-RR: I've edited the answer to use the inspect module so that you wouldn't have to type the file name.
Eli Courtwright
A: 

Not very magical, but:

class A(object):
    def __init__(self)
        self.vanilla_b = B(self)
        self.chocolate_b = B(self)

and:

class B(object):
    def __init__(self, a):
        for i in dir(a):
            if getattr(a, i) == self:
                pass # store it somewhere now

EDIT: Sorry, this won't work. B.init is executed before references in A are set.

lollo
No good, the `B` object should not be getting a reference to `A`.
cool-RR