1

Once I have a specific attribute of an object belonging to some class in Python, how can I get its name as a string? Is this info contained somewhere?

In the following example, suppose I use a.someval in some function: how can I get its name as a string someval just from this value?

class OneClass:
    def __init__(self):
        self.whatIam = 'I am a OneClass instance'
        self.whatIdo = 'Nothing really useful'

    def setSomeVal(self, val):
        self.someval = val

a = OneClass()
a.setSomeVal(13)

If I had the object owning the attribute and not only the attribute value itself, I could go back to its name doing something like:

def present_yourself_knowing_your_owner_object(you, owner):
    print 'My value is', you
    print 'My name is', owner.__dict__.keys()[owner.__dict__.values().index(you)]

present_yourself_knowing_your_owner_object(a.someval, a)
# My value is 13
# My name is someval
present_yourself_knowing_your_owner_object(a.whatIam, a)
# My value is I am a OneClass instance
# My name is whatIam

And I know we can access a method name like this:

print a.setSomeVal.__name__
# setSomeVal

But would it be possible to have the attribute name just from itself, something like:

def present_yourself(you):
    print 'My value is', you
    print 'and my name is', '< ** YOUR SOLUTION HERE ** >'

EDIT to answer @Daniel Roseman's comment:

This can be achieved for some objects, e.g. methods:

def present_yourself(you):
    print 'My value is', you
    print 'and my name is', you.__name__ #'< ** YOUR SOLUTION HERE ;-) ** >'
present_yourself(a.setSomeVal)
# My value is <bound method OneClass.setSomeVal of <__main__.OneClass instance at 0x7f3afe255f38>>
# and my name is setSomeVal
ztl
  • 2,512
  • 1
  • 26
  • 40
  • Generally, you can't do this; objects don't know their names. But even if they did, wouldn't the name here always be `you`? – Daniel Roseman Mar 21 '18 at 11:36
  • Mmh... not if there is some `__name__` method like there is for some objects (e.g. for class methods), or am I wrong? So here I was hoping that an attribute of a class instance would carry that info along with him, but apparently this is not the case... – ztl Mar 21 '18 at 11:39
  • If you know that only one attribute has such value, you can iterate all attributes and find the name. Ugly solution, so I'll not write it down. – Giacomo Catenazzi Mar 21 '18 at 11:45
  • @Giacomo Catenazzi how do you iterate over all attributes if you only have the attribute itself and not the object (nor even the class) it belongs to? – ztl Mar 21 '18 at 11:47
  • e.g. with `vars()`. Check also this, maybe introspection gives you other tools: https://stackoverflow.com/questions/192109/is-there-a-built-in-function-to-print-all-the-current-properties-and-values-of-a – Giacomo Catenazzi Mar 21 '18 at 11:50
  • Note: possibly you can use `is` instead `==` to check if it is the same object. Because objects can be shared, is not definitive, but it rules out the case with same value, but different object. – Giacomo Catenazzi Mar 21 '18 at 11:52
  • Given that `TypeError: vars() argument must have __dict__ attribute`, which is not the case of `a.someval` here, I would still be interested to see how exactly it would work in such case...? Thanks for the ideas! – ztl Mar 21 '18 at 11:58

0 Answers0