On Thu, Jan 02, 2014 at 11:12:30AM +0100, spir wrote: > 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?
py> type(42).__name__ 'int' py> class Spam: ... pass ... py> Spam.__name__ 'Spam' > 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) That works for me. Is there some reason you think it doesn't work? > 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" Completely unnecessary. py> class Ham(Spam): ... pass ... py> Ham.__name__ 'Ham' I think maybe you've made an error somewhere and are misinterpreting what you are seeing. -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor