Passing along a fragment from the psycopg list where Federico Di Gregorio (fog@initd.org) wrote: .... because on python 2.3 PyMapping_Check apperently return True for every new-type instance.... Is this true? If so, is it a bug? Skip
Passing along a fragment from the psycopg list where Federico Di Gregorio (fog@initd.org) wrote:
.... because on python 2.3 PyMapping_Check apperently return True for every new-type instance....
Is this true? If so, is it a bug?
I don't see this. operator.isMappingType() maps directly to PyMapping_Check(); and I tried this: >>> import operator >>> operator.isMappingType(str) False >>> class C(object): pass ... >>> operator.isMappingType(C) False >>> operator.isMappingType(C()) False >>> operator.isMappingType({}) True >>> Looks okay to me. :-) --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum
I don't see this. operator.isMappingType() maps directly to PyMapping_Check(); and I tried this:
>>> import operator >>> operator.isMappingType(str) False >>> class C(object): pass ... >>> operator.isMappingType(C) False >>> operator.isMappingType(C()) False >>> operator.isMappingType({}) True >>>
Looks okay to me. :-)
Are these OK, though:
operator.isMappingType(()) True operator.isMappingType([]) True operator.isMappingType("") True
In Python 2.2:
operator.isMappingType(()) 0 operator.isMappingType([]) 0 operator.isMappingType("") 0
I just found out that this change is the cause for a Bug in my shapelib bindings which at one point try to determine whether a method argument is a dictionary like object by testing whether PyMapping_Check returns true and that assume it's a sequence otherwise. As a work-around it seems I can use isSequenceType/PySequence_Check to effectively turn the test around and determine whether the object is a sequence and assume it's a mapping otherwise. Bernhard -- Intevation GmbH http://intevation.de/ Sketch http://sketch.sourceforge.net/ Thuban http://thuban.intevation.org/
Are these OK, though:
operator.isMappingType(()) True operator.isMappingType([]) True operator.isMappingType("") True
These are because those types have all grown a mapping-style getitem implementation in order to support extended slicing.
I just found out that this change is the cause for a Bug in my shapelib bindings which at one point try to determine whether a method argument is a dictionary like object by testing whether PyMapping_Check returns true and that assume it's a sequence otherwise.
As a work-around it seems I can use isSequenceType/PySequence_Check to effectively turn the test around and determine whether the object is a sequence and assume it's a mapping otherwise.
Still, my suggestion is not to try to guess whether an object is a sequence or a mapping. The two APIs are too similar, and becoming more so in each release. Instead, you should have an explicit indicator passed by the caller (maybe a keyword argument). --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Bernhard Herzog
-
Guido van Rossum
-
Skip Montanaro