RE: Subclasses in Python

Discussion in 'Python' started by Robert Brewer, Apr 29, 2004.

  1. Thomas Philips wrote:
    > I ... used self.__Class__.<whatever I need to access> to access
    > the class attributes in the class and subclass. Works like a charm,
    > but I'm having some difficulty printing class names. I want
    > self.__class__ to return just the name of the class without some
    > ancillary stuff thrown in.


    Try self.__class__.__name__


    FuManChu
    Robert Brewer, Apr 29, 2004
    #1
    1. Advertising

  2. Perfect! But tell me, how did you know to use __name__? I typed
    dir(object) and dir(Player) in IDLE, and in neither case did __name__
    show up.

    >>> dir(object) gives me

    ['__class__', '__delattr__', '__doc__', '__getattribute__',
    '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__setattr__', '__str__']

    dir(Player) gives me a few more things - the object's methods and
    class variables, and some additional stuff: __dict__, __hash__,
    __module__ and __weakref__. Neither sight nor sound of __name__ -
    __module__is as close as I get to the magical __name__. What gives?

    And, before I forget, thanks for all the help - I wouldn't have solved
    it without you.

    Thomas Philips
    Thomas Philips, Apr 30, 2004
    #2
    1. Advertising

  3. Robert Brewer

    Greg Ewing Guest

    Thomas Philips wrote:
    > Perfect! But tell me, how did you know to use __name__? I typed
    > dir(object) and dir(Player) in IDLE, and in neither case did __name__
    > show up.


    Probably it's an oversight on the part of whoever implemented
    the dir() method for classes. I guess you just have to find
    out things like that by hanging out in c.l.py.:)

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, Apr 30, 2004
    #3
  4. Robert Brewer

    Terry Reedy Guest

    "Greg Ewing" <> wrote in message
    news:c6sdiq$fk1bl$-berlin.de...
    > Thomas Philips wrote:
    > > Perfect! But tell me, how did you know to use __name__? I typed
    > > dir(object) and dir(Player) in IDLE, and in neither case did __name__
    > > show up.

    >
    > Probably it's an oversight on the part of whoever implemented
    > the dir() method for classes. I guess you just have to find
    > out things like that by hanging out in c.l.py.:)


    And submit a bug report on sourceforge so some future person will find it
    with dir().

    tjr
    Terry Reedy, Apr 30, 2004
    #4
    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. Bura Tino

    Subclasses in interfaces

    Bura Tino, Nov 3, 2003, in forum: Java
    Replies:
    17
    Views:
    553
    Thomas G. Marshall
    Nov 7, 2003
  2. Praveen
    Replies:
    1
    Views:
    362
    Woebegone
    Nov 21, 2003
  3. Marc Twain
    Replies:
    2
    Views:
    652
    Chris Uppal
    Jan 7, 2004
  4. Steve Nakhla
    Replies:
    0
    Views:
    608
    Steve Nakhla
    Sep 27, 2004
  5. Thomas Philips

    Subclasses in Python

    Thomas Philips, Apr 23, 2004, in forum: Python
    Replies:
    5
    Views:
    13,930
    Terry Reedy
    Apr 29, 2004
Loading...

Share This Page