Introspection Class/Instance Name

Discussion in 'Python' started by *binarystar*, Apr 26, 2006.

  1. *binarystar*

    *binarystar* Guest

    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

    **
     
    *binarystar*, Apr 26, 2006
    #1
    1. Advertising

  2. *binarystar* wrote:
    > 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
     
    Roland Heiber, Apr 26, 2006
    #2
    1. Advertising

  3. *binarystar*

    Guest

    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>
     
    , Apr 26, 2006
    #3
  4. *binarystar*

    Duncan Booth Guest

    *binarystar* wrote:

    > 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'
    >


    >>> class Bollocks(object):

    def name_of_instance(self):
    return "self"


    >>> instance_of_bollocks = Bollocks()
    >>> print instance_of_bollocks.__class__.__name__

    Bollocks
    >>> print instance_of_bollocks.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_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:

    >>> class Bollocks(object):

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


    >>> instance_of_bollocks = Bollocks('Archimedes')
    >>> print instance_of_bollocks.name

    Archimedes
     
    Duncan Booth, Apr 26, 2006
    #4
  5. *binarystar*

    robert Guest

    *binarystar* wrote:
    > 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
     
    robert, Apr 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: class introspection

    John Harrison, Jul 23, 2003, in forum: C++
    Replies:
    4
    Views:
    373
    John Harrison
    Jul 24, 2003
  2. Sridhar R
    Replies:
    14
    Views:
    1,410
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Stephen Nesbitt
    Replies:
    0
    Views:
    274
    Stephen Nesbitt
    Nov 22, 2004
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
  5. EricGoogle
    Replies:
    12
    Views:
    175
    Thomas 'PointedEars' Lahn
    May 19, 2008
Loading...

Share This Page