Get the class name

Robert Kern robert.kern at gmail.com
Sun Jun 7 20:59:12 EDT 2009


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




More information about the Python-list mailing list