[Python-ideas] Introduce collections.Reiterable
mistersheik at gmail.com
Sat Sep 21 02:40:23 CEST 2013
On Fri, Sep 20, 2013 at 8:34 PM, Raymond Hettinger <
raymond.hettinger at gmail.com> wrote:
> On Sep 20, 2013, at 4:59 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> . The glossary might say that the older __getitem__ protocol is
> semi-deprecated (it is no longer used directly) but is adapted for
> back compatibility.
> It is NOT deprecated.
> And I did not suggest that is was. It is, however, not fully supported in
> that collections. Iterable does not recognize __getitem__ iterables and the
> same will be true of code that uses Iterable.
> The collections ABCs are all just a subset of things real collections do.
> For example, there is no slicing support. This was intentional.
> To some degree, the only test of whether something is iterable is to call
> iter() on it and see what happens. With Python's __getattr__ method, the
> only way to test for many behaviors is to attempt to call invoke them to
> see what happens. That is why hasattr() has to invoke getattr().
Is that how you see PEP 3119? It states that the "standardized test" if
something is iterable is precisely to use isinstance(x,
collections.Iterable), which is how I read these paragraphs:
On the other hand, one of the criticisms of inspection by classic OOP
theorists is the lack of formalisms and the ad hoc nature of what is being
inspected. In a language such as Python, in which almost any aspect of an
object can be reflected and directly accessed by external code, there are
many different ways to test whether an object conforms to a particular
protocol or not. For example, if asking 'is this object a mutable sequence
container?', one can look for a base class of 'list', or one can look for a
method named '__getitem__'. But note that although these tests may seem
obvious, neither of them are correct, as one generates false negatives, and
the other false positives.
The generally agreed-upon remedy is to standardize the tests, and group
them into a formal arrangement. This is most easily done by associating
with each class a set of standard testable properties, either via the
inheritance mechanism or some other means. Each test carries with it a set
of promises: it contains a promise about the general behavior of the class,
and a promise as to what other class methods will be available.
This PEP proposes a particular strategy for organizing these tests known as
Abstract Base Classes, or ABC. ABCs are simply Python classes that are
added into an object's inheritance tree to signal certain features of that
object to an external inspector. Tests are done using isinstance(), and the
presence of a particular ABC means that the test has passed.
> Python-ideas mailing list
> Python-ideas at python.org
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas