Cooperative class tree filtering

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 16 22:28:06 EDT 2017


On Mon, Oct 16, 2017 at 8:20 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Oct 12, 2017 at 5:07 PM, Alberto Berti <alberto at metapensiero.it> wrote:
>> I would really like to find a way to do this that doesn't involve
>> decorating the methods in A subclasses to free the final developer to
>> remember to import the decorator and apply it, just like I don't want
>> him (probably me six months from now) to have to remember to add an
>> `else: super()...` to its computation...
>>
>> Has anyone done this before and has any suggestion about a better way to
>> do it? Am I getting it wrong?
>
>
> If you don't want an explicit super() call and you don't want the
> decorator to be explicit either then most likely what you need is a
> metaclass that will automatically wrap specific methods with your
> decorator. Metaclasses are inherited, so all you have to do is set it
> for A and it will automatically apply itselt to B and C as well.


Assuming Python 3.6+ (I see you're already using __set_name__) you
could probably use __init_subclass__ for this as well.



More information about the Python-list mailing list