Better use a class decorator or a metaclass?(was: super not behaving as I expected)
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Sat Apr 4 08:40:42 EDT 2020
Op 29/03/20 om 16:49 schreef Peter Otten:
> Antoon Pardon wrote:
>
>>
>> I have the following program
>>
>> class slt:
>> __slots__ = ()
>>
...
>>
>> class slt1 (slt):
>> __slots__ = 'fld1', 'fld2'
>>
...
>>
>> class slt2(slt1):
>> __slots__ = 'fld3',
>>
....
> Anyway, here's my attempt to collect inherited slots:
>
> @classmethod
> def get_slots(cls):
> all_slots = set()
> for C in cls.__mro__:
> try:
> slots = C.__slots__
> except AttributeError:
> assert C is object
> else:
> all_slots.update(slots)
> return all_slots
>
I have been thinking about this. AFAIU the slots are static data. So it
seems a bit odd to calculate them with a (class) method.
This seems a reasonable opportunity to use a class decorator or a
metaclass. But I am in doubt about which would be the better solution here.
--
Antoon Pardon.
More information about the Python-list
mailing list