[Python-3000] Generic functions vs. OO
Dave Anderson
python3000 at davious.org
Fri Nov 24 15:58:50 CET 2006
on 11/24/2006 12:34 AM Guido van Rossum wrote:
> I would like to end up in a world where, in order to claim that a
> class is a standard mapping class, the class definition would
> (normally) explicitly claim to implement StandardMapping (using as yet
> unspecified syntax)
Guido,
In my world
http://mail.python.org/pipermail/python-3000/2006-November/004736.html
I feel have a usable syntax for you
class StandardMapping:
...
class Foo:
implements StandardMapping
...
> and that there would be a way (again I'm not yet
> specifying syntax for this) to efficiently test any given class or
> instance for that interface, returning True or False.
Foo.does_implement(StandardMapping)
-> True
# based solely on the declaration of Foo implements StandardMapping
> The standard
> dict implementation would claim to implement StandardMapping (and
> probably also something that we could call StandardMutableMapping).
class StandardMutableMapping:
implements StandardMapping
...
dict.implements(StandardMutableMapping)
dict.does_implement(StandardMapping)
-> True
# based on dict's declaration of implementing StandardMutableMapping
# and StandardMutableMapping's declaration of implementing
# StandardMapping
> Someone could define an interface MinimalMapping as the interface
> formed of StandardMapping.getitem, StandardMapping.contains and
> StandardMapping.keys,
MinimalMapping = interface(StandardMapping.getitem,
StandardMapping.contains, StandardMapping.keys)
> and testing a dict for MinimalMapping would
> return True.
dict.does_implement(MinimalMapping)
-> True
# Since dict has declared that it implements StandardMutableMapping
# and StandardMutableMapping has declared it implements StandardMapping
# so, introspection would stop there, since a class implementation
# declaration is pledging that all StandardMapping's methods are
# implemented
> But if someone created ExtendedMapping as StandardMapping
> plus the copy method and the eq operation, testing a dict for
> ExtendedMapping would return False;
ExtendedMapping = interface(StandardMapping, "copy", "__eq__")
dict.does_implement(ExtendedMapping)
-> True
# According to the current implementation of dict
# "copy", "__eq__" are implemented by dict, looking at dir(dict)
# As for StandardMapping, the above declaration chain of dict implements
# StandardMutableMapping and then StandardMutableMapping implements
# StandardMapping then
# dict implements all of ExtendedMapping
> however there should be something
> one could execute to explicitly mark dict as implementing
> ExtendedMapping, using some kind of registry.
dict.implements(ExtendedMapping)
dict.does_implement(ExtendedMapping)
-> True
# Now, since I've explicitly claimed that dict implements
# ExtendedMapping, my introspection stops there.
# the previous introspection described just above has been made
# unnecessary
More information about the Python-3000
mailing list