Get the class name

Jeff McNeil jeff at jmcneil.net
Mon Jun 8 22:18:02 CEST 2009


On Jun 8, 11:33 am, Gary Herron <gher... at islandtraining.com> 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



More information about the Python-list mailing list