Python implements switch with a dictionary. Case-like function call

Source: Internet
Author: User

There is no swich in Python. case, to achieve the same functionality, and do not want to use the IF: Elif to make the best use of the dictionary to achieve

Mainly want to call different methods through different keys, in the learning process, found no matter what the input key is, as long as the dictionary has output content, each time will be output, which is different from the functions that they need.

After debugging, it is found that the problem mainly occurs in the key value corresponding to the method name of the value that has no belt () has a large relationship, as follows:

No matter what number in BB is filled in, it always outputs 123

If CS () is changed to CS, then only 123 will be output when the call is 2, or the return method can be selected without adding ()

Python implements switch with a dictionary. Case-like function call

Related Article

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.