Bob Greschke <[EMAIL PROTECTED]> wrote:
>I miss being able to do something like this in Python
>
>1f (I = a.find("3")) != -1:
>    print "It's here: ", I
>else:
>    print "No 3's here"
>
>where I gets assigned the index returned by find() AND the if statement gets 
>to do its job in the same line.  Then you don't have to have another like 
>that specifically gets the index of the "3".  Is there a way to do this in 
>Python?

It is a deliberate and fundamental design decision in Python that
assignment is a statement, not an expression with side effects.  This
means you often need an "extra" line compared to a classic C "assign
and test" idiom such as you have above.  I, like you, often miss the
compactness of the C idiom, but there it is.

It's also unpythonic to return magic values to indicate failure.
Throwing an exception would be the more normal way of doing it.  So,
I'd expect the above to translate into something like:

try:
    i = a.find("3")
    print "It's here: ", i
except NotFound:
    print "No 3's here"
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to