getting the class name of a subclass

Discussion in 'Python' started by flupke, Jul 20, 2005.

  1. flupke

    flupke Guest

    I have the following test code setup, trying to get the class name of a
    subclass in the super class. (Reason why i want this is described below)

    file class_name_start.py
    ========================
    import class_name as cn

    obj = cn.B()
    obj.printclass()
    ========================


    file class_name.py
    ========================
    class A(object):
    def __init__(self):
    print "I'm A"

    def printclass(self):
    print "Name ",__name__
    print "Class ",A.__name__

    class B(A):
    def __init__(self):
    super(B,self).__init__()
    print "I'm B"
    ========================

    Output:
    I'm A
    I'm B
    Name class_name
    Class A

    I would want the last line to be Class B
    The reason i want this is since i have a number of dialogs all deriving
    from the same super class. In the superclass i have a save function and
    i thought it would be easy to get the classname and write the properties
    in a filename with the classes name as the filename.
    However it turns out i get the class name of the superclass for all.
    All the different dialogs are in seperate files.

    Is there a way to get the name of the subclass. If not i could always
    pass a param to the init function but in my real life code i already
    have several params shipped to the init so i wanted to solve it slightly
    more elegant.

    Regards,
    Benedict
     
    flupke, Jul 20, 2005
    #1
    1. Advertising

  2. "flupke" <> wrote:

    > I have the following test code setup, trying to get the class name of a
    > subclass in the super class. (Reason why i want this is described below)
    >
    > file class_name_start.py
    > ========================
    > import class_name as cn
    >
    > obj = cn.B()
    > obj.printclass()
    > ========================
    >
    >
    > file class_name.py
    > ========================
    > class A(object):
    > def __init__(self):
    > print "I'm A"
    >
    > def printclass(self):
    > print "Name ",__name__
    > print "Class ",A.__name__
    >
    > class B(A):
    > def __init__(self):
    > super(B,self).__init__()
    > print "I'm B"
    > ========================
    >
    > Output:
    > I'm A
    > I'm B
    > Name class_name
    > Class A
    >
    > I would want the last line to be Class B
    > The reason i want this is since i have a number of dialogs all deriving
    > from the same super class. In the superclass i have a save function and
    > i thought it would be easy to get the classname and write the properties
    > in a filename with the classes name as the filename.
    > However it turns out i get the class name of the superclass for all.
    > All the different dialogs are in seperate files.
    >
    > Is there a way to get the name of the subclass. If not i could always
    > pass a param to the init function but in my real life code i already
    > have several params shipped to the init so i wanted to solve it slightly
    > more elegant.
    >
    > Regards,
    > Benedict



    Make printclass a class method:

    class A(object):
    def __init__(self):
    print "I'm A"

    # for python 2.4
    @classmethod
    def printclass(cls):
    print "Module", cls.__module__
    print "Class", cls.__name__
    # for 2.3 or older
    printclass = classmethod(printclass)


    Regards,
    George
     
    George Sakkis, Jul 20, 2005
    #2
    1. Advertising

  3. flupke

    flupke Guest

    George Sakkis wrote:
    <snip>

    > Make printclass a class method:
    >
    > class A(object):
    > def __init__(self):
    > print "I'm A"
    >
    > # for python 2.4
    > @classmethod
    > def printclass(cls):
    > print "Module", cls.__module__
    > print "Class", cls.__name__
    > # for 2.3 or older
    > printclass = classmethod(printclass)
    >
    >
    > Regards,
    > George


    George,

    it works like a charm.

    Thanks!
    Benedict
     
    flupke, Jul 20, 2005
    #3
  4. flupke

    Dan Sommers Guest

    On Wed, 20 Jul 2005 14:06:57 GMT,
    flupke <> wrote:

    > file class_name.py
    > ========================
    > class A(object):
    > def __init__(self):
    > print "I'm A"


    > def printclass(self):
    > print "Name ",__name__
    > print "Class ",A.__name__


    Why "A.__name__" and not "self.__class__.__name__"?

    Regards,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
     
    Dan Sommers, Jul 20, 2005
    #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. jstorta
    Replies:
    3
    Views:
    457
    jstorta
    Feb 20, 2006
  2. Stephen Nesbitt
    Replies:
    0
    Views:
    279
    Stephen Nesbitt
    Nov 22, 2004
  3. S.Volkov
    Replies:
    2
    Views:
    230
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    332
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    403
Loading...

Share This Page