Why would I get a TypeEror?
steven.bethard at gmail.com
Fri Jan 14 16:58:40 EST 2005
It's me wrote:
> Say again???
Please stop top-posting -- it makes it hard to reply in context.
> "Reinhold Birkenfeld" wrote...
>>It's me wrote:
>>>If this is true, I would run into trouble real quick if I do a:
>>Lazy evaluation: use the (x==0 and 1e99 or 1/x) form!
If you want short-circuting behavior, where only one of the two branches
gets executed, you should use Python's short-circuiting boolean
operators. For example,
(x == 0 and 1.0e99 or 1/x)
says something like:
Check if x == 0.
If so, check if 1.0e99 is non-zero. It is, so return it.
If x != 0, see if 1/x is non-zero. It is, so return it.
Note that if you're not comfortable with short-circuiting behavior, you
can also code this using lazy evaluation:
(lambda: 1/x, lambda: 1.0e99)[x==0]()
This says something like:
Create two functions, one to produce 1/x and one to produce 1.0e99.
Select one of these functions depending on whether or not x==0
Invoke the chosen function.
More information about the Python-list