[Python-ideas] check interfaces, isducktype(X, A)

Nick Coghlan ncoghlan at gmail.com
Thu Dec 5 10:46:54 CET 2013


On 5 December 2013 16:48, Gregory Salvan <apieum at gmail.com> wrote:
> Thank you for taking time to answer.
> I trust you, but I've sometimes the feeling we're talking about different
> things.
>
> I didn't know PEP 3124 and PEP 0362, they are great ressources. Signature is
> particularly usefull, it's a pity it's not documented,

It's covered in the inspect module docs:
http://docs.python.org/3/library/inspect#introspecting-callables-with-the-signature-object

> I'm ok with what you said (I talked effectively about signatures).
> The matter I see, is that attributes and methods can be set dynamically
> (that's why I talked about runtime), either I don't manage these cases, or I
> have to extend ABCMeta and override __instancecheck__ and __subclasscheck__.
> The second solution would make duplicate code unless I extract ABCMeta cache
> and registry operations in a dedicated object.

Use ABCMeta.__subclasshook__, that's what it is for.

It's deliberate that ABCs don't check for instance attributes, and
it's only the automatic caching behaviour (required for reasonable
performance) that prevents the ABC machinery from recognising dynamic
class updates by default. However, you *can* make it recognise updates
by invalidating the caches (by doing an explicit registration on any
ABC), and I'd be open to an "abc.invalidate_caches()" function as a
more obvious way to force cache invalidation.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list