Embedding classes' names

Cameron Simpson cs at cskk.id.au
Tue Jul 16 06:57:38 EDT 2019


On 16Jul2019 10:20, Chris Angelico <rosuav at gmail.com> wrote:
>On Tue, Jul 16, 2019 at 10:17 AM DL Neil <PythonList at danceswithmice.info> wrote:
>> When used, do you embed a class's name within its own code, as a 
>> literal?
[...]
>> So, what about other situations where one might need to access the
>> class's own name or that of its/a super-class? eg
>>
>> class C2(C1):
>>         def __init__(self, fred, barney ):
>>                 super().__init__( fred )
>>                 self.barney = barney
>>
>>         def __repr__( self ):
>>                 return f"C2( { self.fred }, { self.barney }"
>>                 ### note: 'common practice' of "C2" embedded as constant
>>
>>
>> How 'purist' do you go, cf YAGNI?
>
>In the case of __repr__, I would most definitely use
>self.__class__.__name__, because that way, a subclass can leave repr
>untouched and still get decent behaviour.

Yeah, me too, though I spell it "type(self).__name__" for no totally 
rational reason.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list