Musings about Python syntax
Gordon McMillan
gmcm at hypernet.com
Mon Oct 25 18:06:34 EDT 1999
[Jim Althoff]
> Where can one find out more info on
> "hasinterface" and "as_sequence"?
[the eff-bot]
> > def len(obj):
> > if hasinterface(obj, as_sequence):
> > return obj.sequence_length()
> > if hasinterface(obj, as_mapping):
> > return obj.mapping_length()
> > raise TypeError, "len() of unsized object"
> >
> >which, for my non-purist eyes, surely looks as if
> >Python treats "obj" as an arbitrary object.
Didn't you snip the bit about it being psuedo-code?
What the eff-bot is alluding to is a call in the abstract level of
the C API, which checks whether the type-as-sequence slot in
the type object is non-NULL. For C types implementing the
sequence methods, this works. Unfortunately, a Python class
emulating a sequence will fail this check.
The types SIG was very actively debating this and similar
topics, until someone posted some code implementing an
interface protocol. All subsequent attempts at reviving the SIG
have failed miserably.
and-the-pathologist-who-did-the-autopsy-died-too-ly y'rs
- Gordon
More information about the Python-list
mailing list