Embedding classes' names
DL Neil
PythonList at DancesWithMice.info
Wed Jul 17 16:08:04 EDT 2019
On 16/07/19 10:57 PM, Cameron Simpson wrote:
> 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.
+1
(looking through my code - perhaps at one time it was 'the old way'?)
--
Regards =dn
More information about the Python-list
mailing list