[Tutor] what's your name? (to a class)

spir denis.spir at gmail.com
Thu Jan 2 11:12:30 CET 2014


Hello tutorians,

Am I missing something or don't classes know how they're called (unlike funcs, 
which have a __name__ attribute, very practicle)? Is there a way to get it 
otherwise?

The point is to have a super-type define a general __repr__ like eg:

class SuperType:
     # ...
     def __repr__ (sef):
         return "%s(stuff)" % (self.__class__.__name__, stuff)

But I need each class to know its name. Subtypes are actually defined by users 
(of a lib), presently they are forced to write the name explicitely, which is 
stupid since they already give it as var name:

class SubType (SuperType):
     __name__ = "SubType"
     # ....

Denis


More information about the Tutor mailing list