[Types-sig] Protocols
Paul Prescod
paulp@ActiveState.com
Tue, 13 Mar 2001 18:20:51 -0800
I agree with Tim that there is no requirement to get the protocols a
hundred percent right. If we make them "too heavyweight" then people
will just choose to not implement all of the methods or will inherit
from UserList to get some for free. Code that depends on those methods
will crash just as it does today.
If we make them "too lightweight", code will depend on methods not
strictly required by the interface and when those methods are missing,
it will crash just as it does today.
So code still crashes at runtime. That's the cost of a dynamically typed
language. BUT we can still help people to avoid passing dictionaries
(mappings) for lists (sequences) and vice versa and that is still better
than nothing.
The minimalist spirit is to lean towards "too lightweight." Therefore I
propose that the protocol for mappings require only __getattr__,
.keys(), .values() and .items(). The protocol for sequences requires
only __getattr__. If someone wants to make a richer set of features on
THEIR sequences or mappings, they can define their own types as Tim
mentioned. Those can be subtypes of the basics we define.
In the future the system will stop guessing at the types of things based
on their methods. If they don't have an actual interface declaration
saying that they are trying to emulate a mapping or sequence (or
whatever), we'll issue a warning that Python doesn't like to guess their
intentions.
In the meantime, we will guess a little. If we see only __getattr__,
we'll presume a sequence.
--
Python:
Programming the way
Guido
indented it.
- (originated with Skip Montanaro?)