[Python-3000] Abilities / Interfaces

Robert Brewer fumanchu at amor.org
Tue Nov 21 21:50:47 CET 2006


Guido van Rossum wrote:
> But it's not so easy to define which APIs must be present to for
> verification of a particular interface to pass. A Mapping must
> implement __getitem__() in a certain way.
> But does it need to implement __len__?

The Ref Manual seems to think so:
http://docs.python.org/ref/types.html#l2h-68:

"The subscript notation a[k] selects the item indexed by k from the
mapping a; this can be used in expressions and as the target of
assignments or del statements. The built-in function len() returns the
number of items in a mapping."

> get()? keys()? values() and items()?

http://docs.python.org/ref/sequence-types.html implies all other methods
are optional (even if heavily encouraged):

"It is also recommended that mappings provide the methods keys(),
values(), items(), has_key(), get(), clear(), setdefault(), iterkeys(),
itervalues(), iteritems(), pop(), popitem(), copy(), and update()
behaving similar to those for Python's standard dictionary objects."

I'd be happy starting with the requirements as stated in the ref
(__getitem__, __setitem__, __delitem__, and __len__, as I read it), and
make any additions or other changes to that contract a separate
discussion. (But then, I'm personally happy with having this API
specified in the ref and don't see a need to codify it with interfaces.)

> Is it okay for a particular mapping implementation to allow
> only one iterator to exist  (per instance) at any time,
> like dbm files do?

Sure, because the current spec doesn't deny it.

If we have to have interfaces, I think we'd be far better off
implementing the spec as-is first, and worry about changing the spec
later (even if if only seems to be an "implied spec" at the moment). I
didn't write the Ref Manual, of course, sir ;) so I can't assert its
intent unequivocally, but as a consumer of it, I expect "it is
recommended" to mean "not an interface requirement".


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org


More information about the Python-3000 mailing list