Get the class name

T

Tuomas Vesterinen

Kless said:
Is there any way of to get the class name to avoid to have that write
it?
.... def cls(self):
.... return self.__class__
....<class '__main__.Foo'>
 
R

Robert Kern

... def cls(self):
... return self.__class__
...
<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
 
G

Gary Herron

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

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
.... pass
....
Foo

Gary Herron
 
J

Jeff McNeil

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top