[Python-3000] iostack and Oh Oh (original) (raw)

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 hasability 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