Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
the PHP ternary operator equivalent on Python
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="David Wahler, post: 1849259"] Well, in your example the '>' operator already returns a boolean value so you can just use it directly. Hoewver, I agree that there are situations in which a ternary operator would be nice. Unfortunately, Python doesn't support this directly; the closest approximation I've found is: This exploits the fact that False and True are converted to integers as zero and one, respectively. The downside is that it's really ugly. Also, it doesn't use minimal evaluation; in other words, if you try an expression like: You might think this would return the value of func() if it's callable, and None otherwise. Unfortunately, func() is evaluated no matter what, even if the condition is false. Of course, you can always get around this by doing really cryptic stuff with lambdas: .... but by that point, you're better off just using an if/then/else. -- David [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
the PHP ternary operator equivalent on Python
Top