Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Introspection Class/Instance Name

Reply
Thread Tools

Introspection Class/Instance Name

 
 
*binarystar*
Guest
Posts: n/a
 
      04-26-2006
Hello there,

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

class *******s:

def __init__( self ):

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


instance_of_*******s = *******s()

# Which outputs

'*******s'
'instance_of_*******s'



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

thx in advance

**
 
Reply With Quote
 
 
 
 
Roland Heiber
Guest
Posts: n/a
 
      04-26-2006
*binarystar* wrote:
> Hello there,
>
> what method would you use to return the name of the class and/or
> instance introspectively eg.
>
> class *******s:
>
> def __init__( self ):
>
> print self.__method_that_returns_class_name__()
> print self.__method_that_returns_instance_name__()
>
>
> instance_of_*******s = *******s()
>
> # Which outputs
>
> '*******s'
> 'instance_of_*******s'
>
>
>
> 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
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      04-26-2006
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>

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      04-26-2006
*binarystar* wrote:

> class *******s:
>
> def __init__( self ):
>
> print self.__method_that_returns_class_name__()
> print self.__method_that_returns_instance_name__()
>
>
> instance_of_*******s = *******s()
>
> # Which outputs
>
> '*******s'
> 'instance_of_*******s'
>


>>> class *******s(object):

def name_of_instance(self):
return "self"


>>> instance_of_*******s = *******s()
>>> print instance_of_*******s.__class__.__name__

*******s
>>> print instance_of_*******s.name_of_instance()

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_*******s
>>> 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:

>>> class *******s(object):

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


>>> instance_of_*******s = *******s('Archimedes')
>>> print instance_of_*******s.name

Archimedes
 
Reply With Quote
 
robert
Guest
Posts: n/a
 
      04-26-2006
*binarystar* wrote:
> Hello there,
>
> what method would you use to return the name of the class and/or
> instance introspectively eg.
>
> class *******s:
>
> def __init__( self ):
>
> print self.__method_that_returns_class_name__()
> print self.__method_that_returns_instance_name__()
>
>
> instance_of_*******s = *******s()
>
> # Which outputs
>
> '*******s'
> 'instance_of_*******s'
>


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/Coo.../Recipe/491265 )

-robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Get a Variable's Name (NOT VALUE) - Introspection???? EricGoogle Javascript 12 05-19-2008 02:25 PM
Introspection Question: Determining subclass name in base class Stephen Nesbitt Python 0 11-22-2004 11:00 PM
Introspection Guidelines for C++? Steven T. Hatton C++ 0 09-03-2004 02:57 AM
Re: class introspection John Harrison C++ 4 07-24-2003 08:45 AM
Actual parameter introspection for error logging David Smith Java 0 07-11-2003 01:32 PM



Advertisments