Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an instance name inside class __init__() [duplicate]

Tags:

While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:

class SomeObject():     defined_name = u""      def __init__(self, def_name=None):         if def_name == None:             def_name = u"%s" % (<INSTANCE NAME>)         self.defined_name = def_name  ThisObject = SomeObject() print ThisObject.defined_name   # Should print "ThisObject" 
like image 566
Akoi Meexx Avatar asked Nov 06 '09 20:11

Akoi Meexx


1 Answers

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.

like image 175
unutbu Avatar answered Sep 19 '22 03:09

unutbu