[Tutor] what's your name? (to a class)
Steven D'Aprano
steve at pearwood.info
Thu Jan 2 14:40:23 CET 2014
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
More information about the Tutor
mailing list