[docs] Bug:(

Georg Brandl georg at python.org
Sat Mar 3 22:59:22 CET 2012


On 03.03.2012 22:28, panax at zmail.lt wrote:
> Hi,
> I have found an interesting thing. Let's compare two _very_ simple and  functionally identical scripts.
> They both check one by one chars in the string s if they are alpha or no:
> =================================================================
> s="aaaaaa"
> print(s)
> if s[0].isalpha():  print("0-yes")
> else:  print("0-no")
> if s[1].isalpha():  print("1-yes")
> else:  print("1-no")
> if s[2].isalpha():  print("2-yes")
> else:  print("2-no")
> if s[3].isalpha():  print("3-no")
> else:  print("3-no")
> if s[4].isalpha():  print("4-no")
> else:  print("4-no")
> if s[5].isalpha():  print("5-yes")
> else:  print("5-no")
> ==============================================
>   - produces (Python 2.7.2, with my comments at the right):
>
> aaaaaa   -the string
> 0-yes    -it's alpha ok
> 1-yes
> 2-yes
> 3-no     -!!!  this 'a' is not alpha!
> 4-no     -!!!  and this also...
> 5-yes    - ok again
>
> Look at pos.3 and 4, those chars now are NOT alpha!
> On the other hand, functionally the same script, but with not direct (not by literal, but by variable j) char addressing, performs ok:
> ==========================================
> s="aaaaaa"
> for j in range(len(s)):
>    if s[j].isalpha():  print(s[j]+"- alpha")
>    else:  print(s[j]+"- Not alpha")
> ===========================================
>
> Seems the behaviour doesn't depend on the string content, but rather on the position of char and the addressing way (literal or no).
>
> Would be glad to be helpful...
> I use Python 2.7.2 (last),  WinXp as a host.
> One more mention - these scripts were running from files, not as dialog session.
> Sorry, if this is known bug, now have no time to check through.
> And don't tell me this is a feature!

It's a feature!

Look again at your code:

 > if s[3].isalpha():  print("4-no")

cheers,
Georg


More information about the docs mailing list