Re: derived class name in python 2.6/2.7

Discussion in 'Python' started by Dave Angel, Jan 30, 2013.

  1. Dave Angel

    Dave Angel Guest

    On 01/30/2013 03:05 PM, Sells, Fred wrote:
    > This is simple, but I just cannot find it after quite a bit of searching
    >
    > I have this basic design
    >
    > class A:
    > def __init__(self):
    > print 'I am an instance of ', self.__class__.name
    >
    > class B(A):
    > pass
    >
    >
    > X = B
    > I would like this to print "I am an instance of B" but I keep getting A. Can someone help me out here.
    >


    Why would creating an alias for class B execute the initializer for
    either class?

    perhaps you meant:

    x = B()

    BTW, since you're on 2.x python, you should derive A from object.
    Otherwise it's an old-style class.

    --
    DaveA
     
    Dave Angel, Jan 30, 2013
    #1
    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. Replies:
    1
    Views:
    422
    myork
    May 23, 2007
  2. Replies:
    1
    Views:
    408
    Victor Bazarov
    May 23, 2007
  3. Goran Djuranovic

    Retrieving derived class name inside base class

    Goran Djuranovic, Sep 10, 2007, in forum: ASP .Net
    Replies:
    8
    Views:
    444
    Goran Djuranovic
    Sep 17, 2007
  4. David
    Replies:
    3
    Views:
    430
    Grizlyk
    Jan 29, 2008
  5. Sells, Fred

    derived class name in python 2.6/2.7

    Sells, Fred, Jan 30, 2013, in forum: Python
    Replies:
    0
    Views:
    141
    Sells, Fred
    Jan 30, 2013
Loading...

Share This Page