Get the class name

Discussion in 'Python' started by Kless, Jun 7, 2009.

  1. Kless

    Kless Guest

    Is there any way of to get the class name to avoid to have that write
    it?

    ---------------
    class Foo:
    super(Foo, self)
    ---------------


    * Using Py 2.6.2
    Kless, Jun 7, 2009
    #1
    1. Advertising

  2. Kless wrote:
    > Is there any way of to get the class name to avoid to have that write
    > it?
    >
    > ---------------
    > class Foo:
    > super(Foo, self)
    > ---------------
    >
    >
    > * Using Py 2.6.2


    >>> class Foo(object):

    .... def cls(self):
    .... return self.__class__
    ....
    >>> Foo().cls()

    <class '__main__.Foo'>
    Tuomas Vesterinen, Jun 7, 2009
    #2
    1. Advertising

  3. Kless

    Robert Kern Guest

    On 2009-06-07 15:41, Tuomas Vesterinen wrote:
    > Kless wrote:
    >> Is there any way of to get the class name to avoid to have that write
    >> it?
    >>
    >> ---------------
    >> class Foo:
    >> super(Foo, self)
    >> ---------------
    >>
    >>
    >> * Using Py 2.6.2

    >
    > >>> class Foo(object):

    > ... def cls(self):
    > ... return self.__class__
    > ...
    > >>> Foo().cls()

    > <class '__main__.Foo'>


    You definitely don't want to use that for super(). If you actually have an
    instance of a subclass of Foo, you will be giving the wrong information.
    Basically, there is no (sane) way to do what the OP wants. If there were, that
    information would not be necessary to give to super().

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Jun 8, 2009
    #3
  4. Kless

    Carl Banks Guest

    On Jun 7, 1:14 pm, Kless <> wrote:
    > Is there any way of to get the class name to avoid to have that write
    > it?
    >
    > ---------------
    > class Foo:
    >    super(Foo, self)
    > ---------------
    >
    > * Using Py 2.6.2


    If you are using emacs you can use the put the following elisp code in
    your .emacs file, defines a command to automatically insert the a
    super call with the class and method name filled in:

    http://code.activestate.com/recipes/522990/


    Carl Banks
    Carl Banks, Jun 8, 2009
    #4
  5. Kless

    Gary Herron Guest

    Kless wrote:
    > Is there any way of to get the class name to avoid to have that write
    > it?
    >
    > ---------------
    > class Foo:
    > super(Foo, self)
    > ---------------
    >
    >
    > * Using Py 2.6.2
    >


    The question does not make sense:
    "to have WHAT write WHAT",
    and the code is wrong:
    the call to super fails
    But even so, perhaps this will answer your question

    >>> class Foo:

    .... pass
    ....

    >>> print Foo.__name__

    Foo

    >>> c = Foo
    >>> print c.__name__

    Foo

    >>> ob = Foo()
    >>> print ob.__class__.__name__

    Foo

    Gary Herron
    Gary Herron, Jun 8, 2009
    #5
  6. Kless

    Jeff McNeil Guest

    On Jun 8, 11:33 am, Gary Herron <> wrote:
    > Kless wrote:
    > > Is there any way of to get the class name to avoid to have that write
    > > it?

    >
    > > ---------------
    > > class Foo:
    > >    super(Foo, self)
    > > ---------------

    >
    > > * Using Py 2.6.2

    >
    > The question does not make sense:
    >     "to have WHAT write WHAT",
    > and the code is wrong:
    >     the call to super fails
    > But even so, perhaps this will answer your question
    >
    >  >>> class Foo:
    > ...   pass
    > ...
    >
    >  >>> print Foo.__name__
    > Foo
    >
    >  >>> c = Foo
    >  >>> print c.__name__
    > Foo
    >
    >  >>> ob = Foo()
    >  >>> print ob.__class__.__name__
    > Foo
    >
    > Gary Herron



    I think the OP wants to call super without having to explicitly name
    the type. If you use the self.__class__ approach in that scenario, you
    can enter into a recursion loop with further inheritance.

    class T(object):
    """I'm a standard class"""
    def f(self):
    print "function"

    class S(T):
    """I call super using self.__class__"""
    def f(self):
    super(self.__class__, self).f()

    class J(S):
    """I don't know about S' odd call."""

    j = J()
    j.f() # <--- Bombs
    Jeff McNeil, Jun 8, 2009
    #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. =?Utf-8?B?SG1udA==?=
    Replies:
    5
    Views:
    2,633
    IcingDeath
    Jun 25, 2006
  2. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  3. ding feng
    Replies:
    2
    Views:
    2,792
    ding feng
    Jun 25, 2003
  4. Peng Yu
    Replies:
    2
    Views:
    364
  5. encapsul

    how to get child class "name" from parent class

    encapsul, Sep 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    351
    Brock Allen
    Sep 15, 2005
Loading...

Share This Page