På 14. jan 2005 kl. 22:58 skrev Steven Bethard:
(Any mac users? How do I fix this to appear in Norwegian? =)
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]()
.. and people wonder why so many Python people want to get rid of Lambda =)
Heh heh. No I don't. ;)
In fact, I don't ever use lambdas in any of my own "real" code. But I don't mind being a little dirty when I post to c.l.py. ;) I guess I could have written this as:
def inverse(): return 1/x def largenum(): return 1.0e99 b = (inverse, largenum)[x==0]()
but I'm usually too lazy, and it's an ugly solution anyway, compared to the simple one which the OP was apparently trying to avoid:
if x != 0: b = 1/x else: b = 1.0e99
=)
Steve -- http://mail.python.org/mailman/listinfo/python-list