"?:", "a and b or c" or "iif"

William Tanksley wtanksle at dolphin.openprojects.net
Thu May 27 20:00:03 EDT 1999


On 27 May 1999 08:27:53 +0200, Hrvoje Niksic wrote:
>"Tim Peters" <tim_one at email.msn.com> writes:

>> No, -O doesn't do much:  binds the builtin name __debug__ to 0 and skips
>> generating code for blocks of the form "if __debug__: xxx"; as a conceptual
>> consequence of that, skips generating code for "assert" stmts;

>?!?

>I had no idea about asserts and -O!  Currently I'm using `assert' to
>ensure logical correctness of some parts of the code before continuing
>execution so that the system doesn't wind up in an in consistent
>state, for instance:

That's good -- this is exactly what you're supposed to do.  "assert" is
supposed to state that the following code _requires_ a given condition.
It's actually a comment, but it's checked by the computer.  Because it's
only a comment, throwing it away is the right thing to do when making
production code.

Warning: never put error detection code in an assert!  Assertions are only
to be used to detect programming mistakes.

Bertrand Meyer calls this "design by contract" -- essentially, he says,
each function has a list of conditions it expects when it starts, and each
function promises to do certain things.  Assertions make sure that when
you're programming you keep your contracts.  But you don't want to leave
them in there to bother the user.

>> -OO goes on to throw away docstrings.

>Yuck!

Pardon?  What?

>> (x and (a,) or (b,))[0]-is-faster-ly y'rs  - tim

>I'm curious -- why?  Is creating and referencing a tuple really faster 
>than creating and referencing a list?

Always.  That's one reason we have tuples.

-- 
-William "Billy" Tanksley
"But you shall not escape my iambics."
           -- Gaius Valerius Catullus




More information about the Python-list mailing list