A C-like if statement

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Feb 24 04:58:33 EST 2006


On Thu, 23 Feb 2006 16:49:09 -0800, bonono wrote:

> 
> Steven D'Aprano wrote:
>> On Thu, 23 Feb 2006 12:04:38 -0700, Bob Greschke wrote:
>>
>> >> try:
>> >>    i = a.find("3")
>> >>    print "It's here: ", i
>> >> except NotFound:
>> >>    print "No 3's here"
>> >
>> > Nuts.  I guess you're right.  It wouldn't be proper.  Things are added or
>> > proposed every day for Python that I can't even pronounce, but a simple 'if
>> > (I = a.find("3")) != -1' isn't allowed.  Huh.  It might be time to go back
>> > to BASIC. :)
>>
>> There are *reasons* why Python discourages functions with side-effects.
>> Side-effects make your code hard to test and harder to debug.
>>
> 
>>>> "test".index("a")
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in -toplevel-
>     "test".index("a")
> ValueError: substring not found
>>>> "test".find("a")
> -1


Did you have a point?

In case you haven't been following the entire thread, the original bit of
code above (the try block using the find method) wasn't mine. It just
happened to be quoted in my post.


Now that's a Python wart: using >>> for the prompt for interactive
sessions. It makes it ambiguous when posting code in email or newsgroups,
especially once the code gets quoted a few times.


-- 
Steven.




More information about the Python-list mailing list