Characters contain themselves?
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Fri Apr 7 20:43:11 EDT 2006
On Fri, 07 Apr 2006 15:50:53 +0200, WENDUM Denis 47.76.11 (agent) wrote:
> While testing recursive algoritms dealing with generic lists I stumbled
> on infinite loops which were triggered by the fact that (at least for my
> version of Pyton) characters contain themselves.
A "character" is just a string of length 1.
[snip]
> Leading to paradoxes and loops objects which contain themselves (and
> other kinds of monsters) are killed in set theory with the Axiom of
> Foundation:=)
Let me see if I understand you... you would like behaviour like this?
>>> 'a' in 'bbabb'
True
>>> 'a' in 'bbab'
True
>>> 'a' in 'bab'
True
>>> 'a' in 'ab'
True
>>> 'a' in 'a'
False # not the real result
Ain't going to happen.
> 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.
I think that if you want to do work with sets, use sets, not strings.
In Python 2.3, you need:
>>> import sets
In Python 2.4, Sets (note the initial capital) are a built-in.
--
Steven.
More information about the Python-list
mailing list