|
|
|
"""it's reasonable to test for string like objects using isinstance(obj, str)."""
The test should be for 'basestring'. Unicode is not a subclass of str.
Andrew Dalke |
Homepage |
05/11/14 - 10:33 am | #
|
|
I am not too sure about this - I mean, what if I want an object be both a sequence and a mapping type?
I might be talking gibberish here, but for example I would like to map a number to a character in a string and also be able to iterate trough all characters of the string.
Aigars Mahinovs |
05/11/14 - 11:06 am | #
|
|
"""The test should be for 'basestring'. Unicode is not a subclass of str."""
Yes - quite right.. oops.
Thanks
Fuzzyman |
Homepage |
05/11/14 - 1:54 pm | #
|
|
"""I am not too sure about this - I mean, what if I want an object be both a sequence and a mapping type?"""
Then passing it to a function or method that treats sequences and mapping type objects differently is asking for trouble !
An ordered dictionary is an example of an object that behaves (a bit) like both a sequence and a mapping. I thought of implemnting integer indexing for ConfigObj - which is effectively an ordered dictionary.

Fuzzyman |
Homepage |
05/11/14 - 5:04 pm | #
|
|
I don't think this causes trouble for objects that support both mapping and sequence interfaces. IsMappingType(obj)==True does not imply that IsSequenceType(obj)==False, nor should it.
Matt |
05/11/14 - 6:14 pm | #
|
|
Check out Dulcinea, just the spec module.
http://www.mems-exchange.org/sof...tware/dulcinea/
View the source:
http://www.mems-exchange.org/sof....11/lib/
spec.py
It would be worth while downloading Dulcinea just to see how "specifications" are used. its much lighter weight than Interfaces.
Of interest - there is a mapping spec which allows you to do something like this:
mapping({int:Keyed}, either(PersistentDict, BTree, {}))
or
sequence(either(int, string), [])
or
sequence(either(int, string), either([], ()))
or
sequence(SomeObject, [])
Its really powerful yet really simple.
Michael Watkins |
Homepage |
05/11/21 - 3:34 pm | #
|
|
"I don't think this causes trouble for objects that support both mapping and sequence interfaces. IsMappingType(obj)==True does not imply that IsSequenceType(obj)==False, nor should it."
What objects do you have in mind ?
And what about ``obj[0]`` - if an item is both a sequence and a mapping type, does that operation fetch the item indexed by position zero or the item keyed by the integer zero ?
They are certainly not fully compatible.
Fuzzyman
Fuzzyman |
Homepage |
05/11/23 - 9:27 am | #
|
|
|
Commenting by HaloScan
|