Testing for sequence types

Jim Althoff jima at aspectdv.com
Mon Oct 25 18:23:48 EDT 1999


Thanks for the clarification and interesting
info on past discussions.

So then, for testing Python classes (as opposed
to just C extension classes) is

hasattr(anObject,"get_item")

considered good form for testing "sequence-ness"?

Or is there a more preferred idiom?

Thanks,

Jim


At 06:06 PM 10/25/99 -0400, Gordon McMillan wrote:
>[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