[Python-Dev] Is X a (sequence|mapping)?

M.-A. Lemburg mal@lemburg.com
2001年1月23日 10:15:11 +0100


"Martin v. Loewis" wrote:
>> > i don't see an easy way to test if something supports one of these
> > abstract interfaces in Python.
>> Why do you want to test for that? If you have an algorithm that only
> operates on integer-indexed things, what can you do if the test fails?
>> So it is always better to just use the object in the algorithm, and
> let it break with an exception if somebody passes a bad object.

Right. 
Polymorphic code will usually get you more out of an 
algorithm, than type-safe or interface-safe code.
BTW, there are Python interfaces to PySequence_Check() and
PyMapping_Check() burried in the builtin operator module in case
you really do care ;) ...
	operator.isSequenceType()
	operator.isMappingType()
	+ some other C style _Check() APIs
These only look at the type slots though, so Python instances
will appear to support everything but when used fail with
an exception if they don't provide the proper __xxx__ hooks.
-- 
Marc-Andre Lemburg
______________________________________________________________________
Company: http://www.egenix.com/
Consulting: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/

AltStyle によって変換されたページ (->オリジナル) /