The Voidspace Techie Blog

Gravatar """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.


Gravatar 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.


Gravatar """The test should be for 'basestring'. Unicode is not a subclass of str."""

Yes - quite right.. oops.

Thanks


Gravatar """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.



Gravatar 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.


Gravatar 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.


Gravatar "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


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan