Giter VIP home page Giter VIP logo

blog's People

Stargazers

 avatar

Watchers

 avatar

blog's Issues

Python Ternary Operators and A Related Misconception

In Java and C, the expression <condition> ? <expression True> : expression False is widely used as an inline format. It could make the logic more clear than multi-line format for guys who understand this convention. Actually, there is a logically equivalent expression in Python, which is <expression True> if <condition> else <expression False>. However, I found a number of articles that have partial misconception, in which, they hold the argument that the expression <expression True> if <condition> else <expression False> is logically equivalent to the expression <condition> and <expression True> or <expression False>. The aim of this short post is to conduct a comparison between the two expressions and explain why the second expression is not logically equivalent to the first one.

Python logical expressions

This is the link to the Documentation of Boolean operations in Python3.

For convenience, I put the quotation here.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Experiments

In this case, it seems that the expression <condition> and <expression True> or <expression False> can be used as a ternary operator. However, there is still an exception. Let's look at the code block.

>>> (2 > 1) and 0 or 1
1
>>> (2 > 1) and 2 or 1
2
>>> (2 > 1) and 3 or 2
3

Obviously, the expression (2 > 1) returns True, the first line should output 0 if this expression is logically equivalent to the expression <expression True> if <condition> else <expression False>. However, the output of the second line and the third line seem reasonable.

Explanation

In the first experiment (2 > 1) and 0 or 1, after calculating the value of (2 > 1), the Python interpreter evaluates the first operand of the and operator, which is True and then returns the resulting value, which is False. Since 0 and True returns False. After that, the interpreter evaluates the expression False or 1, which returns 1.

Conclusion

The logical expression <condition> and <expression True> or <expression False> could be used as a tricky way as a ternary operator. However, the edge case must be taken into consideration. The recommend way of handling this scenario is to use the expression <expression True> if <condition> else <expression False>, which is also officially recommended. However, the returning rules could also be used for other cases, for example or operator can be use to assign a default value, an example is as follows:

# when user.get_user_name() returns None, user_name will be assigned a default value.
user_name = user.get_user_name() or "Anonymous User"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.