Characters contain themselves?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Apr 7 21:09:45 EDT 2006


On Fri, 07 Apr 2006 15:50:53 +0200, WENDUM Denis 47.76.11 (agent) wrote:

> But let's go back to more earthly matters. I couldn't find any clue in a 
> python FAQ after having googled with the following "Python strings FAQ" 
> about why this design choice and how to avoid falling in this trap 
> without having to litter my code everywhere with tests for stringiness 
> each time I process a generic list of items.
> 
> Any hints would be appreciated.

Here is another suggestion.

If you absolutely must use strings rather than sets or lists, create a
custom string class:

>>> class Mystr(str):
...     def __contains__(self, other):
...             if self == other: return False
...             return super(Mystr, self).__contains__(other)
...
>>> s = Mystr("hello world")
>>> "x" in s
False
>>> "w" in s
True
>>> "hello" in s
True
>>> "hello world" in s
False


Hope this helps.


-- 
Steven.




More information about the Python-list mailing list