[Python-3000] iostack and Oh Oh

Steven Bethard steven.bethard at gmail.com
Thu Dec 7 23:13:24 CET 2006


On 12/7/06, Tim Hochberg <tim.hochberg at ieee.org> wrote:
> Let me throw out an examples of how this could look.
>
>    @implements(sequence.__getitem__, sequence.__len__)
>    class MySequence:
>        #....
>
>    @implements(mapping.__getitem__, mapping.keys, mapping.__len__)
>    class MyMapping:
>        #....
>
>    #...
>
>    # supports reads better than has_ability to me in this context.
>    if supports(someobject, mapping.__getitem__):
>       # do something


FWLIW, I like the concept of a ``supports()`` method like this, and I
think it shouldn't be too foreign to Python users syntactically.  I'm
sure I've seen code like this before::

    if hasattr(someobject, '__getitem__'):
        # do something

And the only real difference between the ``hasattr()`` code and the
``supports()`` code is that the ``supports()`` code not only
guarantees the existence of the method, but guarantees that the method
conforms to the expected semantics.

For the sake of DRY, I'd prefer to see::

    class MyMapping:
        def mapping.__getitem__(self, key):
            ...
        def mapping.__len__(self):
            ...

instead of::

    @implements(mapping.__getitem__, mapping.__len__)
    class MyMapping:
        def __getitem__(self, key):
            ...
        def __len__(self):
            ...

but since we don't have syntactic support for either of these variants
right now, I think what would be most helpful is a proof-of-concept
patch that worked something like::

    class MyMapping:
        def __getitem__(self, key):
            ...
        def __len__(self):
            ...
    implements(MyMapping, mapping.__getitem__, mapping.__len__)


STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list