Getting an instance name inside class __init__()

Well, there is almost a way to do it:

#!/usr/bin/env python
import traceback
class SomeObject():
    def __init__(self, def_name=None):
        if def_name == None:
            (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
            def_name = text[:text.find('=')].strip()
        self.defined_name = def_name

ThisObject = SomeObject()
print ThisObject.defined_name 
# ThisObject

The traceback module allows you to peek at the code used to call SomeObject(). With a little string wrangling, text[:text.find('=')].strip() you can guess what the def_name should be.

However, this hack is brittle. For example, this doesn't work so well:

ThisObject,ThatObject = SomeObject(),SomeObject()
print ThisObject.defined_name
# ThisObject,ThatObject
print ThatObject.defined_name 
# ThisObject,ThatObject

So if you were to use this hack, you have to bear in mind that you must call SomeObject() using simple python statement:

ThisObject = SomeObject()

By the way, as a further example of using traceback, if you define

def pv(var):
    # stack is a list of 4-tuples: (filename, line number, function name, text)
    # see http://docs.python.org/library/traceback.html#module-traceback
    #
    (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
    # ('x_traceback.py', 18, 'f', 'print_var(y)')
    print('%s: %s'%(text[text.find('(')+1:-1],var))

then you can call

x=3.14
pv(x)
# x: 3.14

to print both the variable name and its value.


Instances don't have names. By the time the global name ThisObject gets bound to the instance created by evaluating the SomeObject constructor, the constructor has finished running.

If you want an object to have a name, just pass the name along in the constructor.

def __init__(self, name):
    self.name = name