Python: Getting the attribute name that the created object will be given
Posted
by cool-RR
on Stack Overflow
See other posts from Stack Overflow
or by cool-RR
Published on 2010-04-09T15:25:12Z
Indexed on
2010/04/09
15:33 UTC
Read the original article
Hit count: 285
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?
© Stack Overflow or respective owner