Introspection Class/Instance Name

B

*binarystar*

Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()


instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'



I have been scouring the 2.4 docs ... I am sure it is frustratingly simple

thx in advance

**
 
R

Roland Heiber

*binarystar* said:
Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()


instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'



I have been scouring the 2.4 docs ... I am sure it is frustratingly simple

thx in advance

**

Hi,

take a look at self.__class__.__name__ for the Class-name.

HtH, Roland
 
W

wittempj

What about:

py> class A:
py. def __init__(self):
py. print self.__class__.__name__
py. print str(self)
py. def __str__(self):
py. return 'instance of %s' % self.__class__.__name__
py.
py> a = A()
A
instance of A
py>
 
D

Duncan Booth

*binarystar* said:
class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()


instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'
def name_of_instance(self):
return "self"


At the time when the method is called, 'self' is a perfectly valid name for
the instance. Seriously though, how do you expect a method to decide if you
do:
another_name = instance_of_bollocks
print another_name.name_of_instance() ??? which name should appear here ???
more = [another_name]*5
print more[2]
??? and what name here ???

and did you want a global name, or a local variable from some function and
if so which function and at which stack level?

Python does actually give you sufficient introspection to do this, but you
have to write some fairly complex code to iterate through the namespaces
you are interested in searching for the object.

A much more reasonable solution is to give your object a name attribute:
def __init__(self, name):
self.name = name

Archimedes
 
R

robert

*binarystar* said:
Hello there,

what method would you use to return the name of the class and/or
instance introspectively eg.

class Bollocks:

def __init__( self ):

print self.__method_that_returns_class_name__()
print self.__method_that_returns_instance_name__()


instance_of_bollocks = Bollocks()

# Which outputs

'Bollocks'
'instance_of_bollocks'

self.__class__ is good for getting instance's top class

yet Python is weak on introspecting the namespace of its definitions -
funcs and classes. it stops at sys._getframe().f_code.co_name

thus, e.g. for things like super() you need always to re-type the class
name where you just write in!?

maybe Py3K brings more ? Maybe There could be compiler-variables like
__CLASS__ , __FUNC__ for things like super(), recursion etc.
(compare http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491265 )

-robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top