Better use a class decorator or a metaclass?(was: super not behaving as I expected)

Souvik Dutta souvik.viksou at gmail.com
Sat Apr 4 09:30:03 EDT 2020


I think this should help
https://stackoverflow.com/questions/1779372/python-metaclasses-vs-class-decorators

On Sat, 4 Apr, 2020, 6:12 pm Antoon Pardon, <antoon.pardon at rece.vub.ac.be>
wrote:

> 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.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list