[Python-3000] Adaptation [was:Re: Iterators for dict keys, values, and items == annoying :)]

Jim Jewett jimjjewett at gmail.com
Sat Apr 1 01:47:45 CEST 2006


On 3/30/06, Alex Martelli <aleaxit at gmail.com> wrote:
> Consider __index__, and a user of gmpy, assuming
> gmpy didn't rush out a 2.5 release with tp_index support.
> The user of gmpy would be stuck -- no way he could
> use a gmpy.mpz as an index into a list, because
> the ad-hoc-adaptation of __index__ means that the type
> itself must grow the slot.

So the adaptation would be

    def gmpy_mpz_index(self):
        return long(self)
    gmpy.mpz.__index__=gmpy_mpz_index

Monkeypatching someone else's class may not be pretty, but adaptation
is basically a standing offer to (less efficiently) pretend you did. 
If neither the caller nor the callee makes an explicit call to the
adaption machinery, then you might as well change the original class.

At the moment, some classes throw an TypeError if you try to add
attributes later.  Are you suggesting that this behavior should be
removed?  (I'll grant that it isn't *usually* helpful.)

What about updates, rather than inserts?  Should I be able to replace
dict.__getitem__ and have it affect all existing dicts?

(And if this thread continues, should it move to python-dev?)

-jJ


More information about the Python-3000 mailing list