Finding the name of a class

Discussion in 'Python' started by Edward C. Jones, May 13, 2004.

  1. Is there a function or method that returns the name of a class or class
    instance?

    class X(object):
    pass

    X.amethod() or X().amethod() should return the string "X".

    X().__class__ returns "<class '__main__.X'>" which I could parse. Ugh.

    Or I could use module pyclbr. Ugh**2.
     
    Edward C. Jones, May 13, 2004
    #1
    1. Advertising

  2. Edward C. Jones

    Paul McNett Guest

    Edward C. Jones writes:

    > X().__class__ returns "<class '__main__.X'>" which I could
    > parse. Ugh.


    How about X().__class__.__name__

    --
    Paul
     
    Paul McNett, May 13, 2004
    #2
    1. Advertising

  3. Edward C. Jones

    Ryan Paul Guest

    On Wed, 12 May 2004 20:10:07 -0400, Edward C. Jones wrote:

    > Is there a function or method that returns the name of a class or class
    > instance?
    >
    > class X(object):
    > pass
    >
    > X.amethod() or X().amethod() should return the string "X".
    >
    > X().__class__ returns "<class '__main__.X'>" which I could parse. Ugh.
    >
    > Or I could use module pyclbr. Ugh**2.



    I havent found any better way to do it:

    def classname(c):
    sl = `c`.split(' ')[0][1:].split('.')
    return len(sl) > 1 and sl[1] or sl[0]
     
    Ryan Paul, May 13, 2004
    #3
  4. "Edward C. Jones" wrote:

    > Is there a function or method that returns the name of a class or
    > class
    > instance?
    >
    > class X(object):
    > pass
    >
    > X.amethod() or X().amethod() should return the string "X".
    >
    > X().__class__ returns "<class '__main__.X'>" which I could parse. Ugh.
    >
    > Or I could use module pyclbr. Ugh**2.


    Use the __name__ attribute:

    >>> class C: pass

    ....
    >>> C.__name__

    'C'
    >>> C().__class__.__name__

    'C'

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Without love, benevolence becomes egotism.
    -- Dr. Martin Luther King, Jr.
     
    Erik Max Francis, May 13, 2004
    #4
  5. Erik Max Francis wrote:
    > "Edward C. Jones" wrote:
    >>Is there a function or method that returns the name of a class or
    >>class
    >>instance?
    >> ...

    >
    > ...
    > Use the __name__ attribute:



    Thanks. Once I knew the answer, I had no trouble finding it in the
    documentation. See Library Reference, 3.11.1 or Reference Manual 3.2. I
    didn't find it because "__name__" is not listed in dir(X) or dir(X()).
     
    Edward C. Jones, May 13, 2004
    #5
  6. I had exactly the same problem, and was given the __name__ solution by
    members of this newsgroup. I then posted a bug report to Sourceforge,
    and got a reply from Python's maintainer's: the inability of
    dir(object) to display __name__ is not a bug, but a natural
    consequence of the way dir()is implemented. The responses to my bug
    report follow:

    Thomas Philips
    --------------------------------------------------------------------------------
    Date: 2004-05-11 22:55
    Sender: fdrake
    Logged In: YES
    user_id=3066

    I'm not convinced that attributes dynamically provided by
    __getattr__() aren't actual attributes; it would be best if dir()
    reported them if they're available via getattr(ob, name). Whether or
    not this is practical is another matter.

    I've just closed documentation bug #952212, so at least the presence
    of the __name__ attribute on types and classes is mentioned somewhere.

    I'm re-classifying this bug report, since the dynamic behavior of
    dir() is not a documentation issue.


    --------------------------------------------------------------------------------

    Date: 2004-05-02 15:47
    Sender: montanaro
    Logged In: YES
    user_id=44345

    After a message from the submitter, it's apparent he was referring to
    class objects not showing '__name__' attributes in dir() output. This
    is a case of an attribute not being visible to dir() because it's not
    directly present in the object's __dict__ and is trapped at evaluation
    time by __getattr__(). Short of hacking dir() or adding a special
    attribute ("__attributes__"?) to objects which have __getattr__()
    methods I don't see a way around this problem.

    Wasn't there discussion of such an attribute which would expose such
    dynamic attributes to dir()? I don't see anything in the
    implementation of PyObject_Dir().



    --------------------------------------------------------------------------------

    Date: 2004-05-01 21:02
    Sender: montanaro
    Logged In: YES
    user_id=44345

    Are you sure that the object has an actual __name__ attribute (and not
    something computed by a __getattr__ method)?

    >>> import sys
    >>> dir(sys)

    ['__displayhook__', '__doc__', '__excepthook__', '__name__',
    '__stderr__', '__stdin__', '__stdout__', '_getframe', 'api_version',
    'argv', 'builtin_module_names', 'byteorder', 'call_tracing',
    'callstats', 'copyright', 'displayhook', 'exc_clear', 'exc_info',
    'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit',
    'exitfunc', 'getcheckinterval', 'getdefaultencoding',
    'getdlopenflags', 'getfilesystemencoding', 'getrecursionlimit',
    'getrefcount', 'hexversion', 'maxint', 'maxunicode', 'meta_path',
    'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform',
    'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
    'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin',
    'stdout', 'version', 'version_info', 'warnoptions']
    >>> sys.__name__

    'sys'
     
    Thomas Philips, May 13, 2004
    #6
    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. E11
    Replies:
    1
    Views:
    4,937
    Thomas Weidenfeller
    Oct 12, 2005
  2. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    890
    Roedy Green
    Jan 2, 2006
  3. ding feng
    Replies:
    2
    Views:
    2,904
    ding feng
    Jun 25, 2003
  4. Kirk Strauser

    Finding the name of a class

    Kirk Strauser, Aug 1, 2006, in forum: Python
    Replies:
    21
    Views:
    634
    John Salerno
    Aug 3, 2006
  5. Bobby Chamness
    Replies:
    2
    Views:
    2,440
    Joe Smith
    Apr 22, 2007
Loading...

Share This Page