Solve the problem that ironpython does not have a ternary Operator

Source: Internet
Author: User

Today, my colleagues use Lambda in ironpython to writeProgram(Our program uses the lambda function of ironpython) and finds a problem. Suppose there is a function: c = A/B, but B may be 0. If it is 0, so we want c = 0. Because it is a Lambda expression, we must use a line of description. Unfortunately, I found that ironpython does not support the ternary operator. Later I checked the information, I found that snowdream wrote a solution: Python Study Notes (3 ).
The modified program is:B! = 0 and A/B or 0Note that the difference is not equal here. We find that when B is 0, it does not calculate a/B, which serves our purpose.

Supplement

Today, I encountered this kind of problem again. I searched the network again and found that there was actually a smooth writing:

Result = A/B if B <> 0 else 0

That is, if B is not equal to 0, the calculation expression. Otherwise, 0 is returned.

Supplement:

I am so stupid. Someone has pointed out this method for replying last year. I have not paid attention to it yet. Whining, hitting your ass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.