[Python-Dev] PEP 443 - Single-dispatch generic functions (including ABC support)

Nick Coghlan ncoghlan at gmail.com
Sat May 25 17:13:00 CEST 2013


On Sun, May 26, 2013 at 12:53 AM, Łukasz Langa <lukasz at langa.pl> wrote:
> On 25 maj 2013, at 16:08, PJ Eby <pje at telecommunity.com> wrote:
>
>> ISTM there should be some way to get at the raw
>> registration info, perhaps by exposing a dictproxy for the registry.
>
> Is that really useful? Just today Antoine asked about changing
> behaviour of __subclasses__(), suspecting it isn't used in real world
> code anyway. What you're proposing is the functional equivalent of
> __subclasses__().
>
> If you need direct access to the registry, do you think the ability to
> specify your own registry container isn't enough?

I'm actually wary about allowing that - letting people pass in the
registry namespace the can alter it directly and thus bypass any extra
code in register (such as setting the flag to enable the ABC support
in the reference impl, or clearing the cache in PJE's suggested
update).

We don't allow custom namespaces for classes either. Sure, we allow
them for the class *body*, but the contents of that get copied to a
new dict when creating the class instance, precisely so we can ensure
user code never gets a direct reference to the underlying mapping that
defines the class behaviour.

I actually patched Python once to to remove that copy operation while
tinkering with the PEP 422 implementation. I wish I had kept a
recording of the subsequent terminal session, as the terrible
interactions with the method lookup caching were thoroughly confusing,
but quite entertaining if you're into tinkering with programming
languages :)

So I think I'd prefer flipping this around - you can't provide a
custom registry mapping, but you *can* get access to a read only view
of it through a "registry" attribute on the generic function.

>> The way to detect ABC registrations is via the
>> ABCMeta._abc_invalidation_counter attribute: if its value is different
>> than the previous value saved with the cache, the cache must be
>> cleared, and the new value stored.
>
> Wow, I was looking at it just today morning and somehow missed how it
> can be used. Great idea, I'll play around with it later today.

Yeah, this is exactly how the ABC code itself uses it - sorry I wasn't
clearer about why I suggested you look at the caching code for ABC
instance and subtype checks :)

Cheers,
Nick.

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


More information about the Python-Dev mailing list