order of test

Andrew McNamara andrewm at object-craft.com.au
Mon Jul 29 04:37:22 EDT 2002


>can we be sure of the order of a test expression ?
>i mean that
>t=["one","two"]
>x=10
>if x<2 and t[x]=="oups":
>
>will never throw an exception if t[2] will be tested before x<2

It's called lazy evaluation, and python shares this trait with C. You can
see it in action with something like:

    >>> def foo():
    ...     print "foo" 
    ...     return 1
    ... 
    >>> if 1 and foo():
    ...     print "bar"
    ... 
    foo
    bar
    >>> if 0 and foo():
    ...     print "bar"
    ... 
    >>>

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/




More information about the Python-list mailing list