Characters contain themselves?

WENDUM Denis 47.76.11 (agent) denis.wendum at pasdepourriels.edf.fr
Mon Apr 10 09:18:39 EDT 2006



gry at ll.mit.edu wrote:
> In fact, not just characters, but strings contain themselves:
> 
> 
>>>>'abc' in 'abc'
> 
> True
> 
> This is a very nice(i.e. clear and concise) shortcut for:
> 
> 
>>>>'the rain in spain stays mainly'.find('rain') != -1
> 
> True
> 
> Which I always found contorted and awkward.
> 
> Could you be a bit more concrete about your complaint?
> 
> -- George
> [Thanks, I did enjoy looking up the Axiom of Foundation!]
Here is the minimal context triggering an infinite descent in recursion.
 From the answers I've got it seems I'll have to check if I'm iterating
on a string or on another kind of "list".
> python
Python 2.3.5 (#2, Feb  9 2005, 00:38:15)
[GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.setrecursionlimit(10)
>>> def f(List):
...       try: # assuming List supports iteration
...           new=[f(item) for item in List]
...       except:# if no iteration is possible
...           new=1
...       return new
...
>>> f([1, [2,3]]) # f substitutes each non iterable item by 1 (silly butshows the problem)
[1, [1, 1]]
>>> f([1, [2.3, (5,6,7)]])
[1, [1, [1, 1, 1]]]
>>> f('bac')
[[[[[[[[1]]]]]]], [[[[[[[1]]]]]]], [[[[[[[1]]]]]]]]
>>> sys.setrecursionlimit(5)
>>> f('bac')
[[[1]], [[1]], [[1]]]
>>> # each item in 'bac' is a character, 
ie. a string of length one on wich one can iterate
and so triggers an infinite descent of recursion.
  ...




More information about the Python-list mailing list