Python function _ Adorner

Source: Internet
Author: User

Understand:

Decorator: I've seen such an analogy (our function is like underwear, the effect is modesty. But in some specific circumstances, underwear obviously can't meet our needs, winter it can not wind and cold for us. Therefore has the trousers, the ornament is like the trousers, does not affect the underwear function The premise, gives our body to provide the warm effect.

An adorner is an extension of the original function, essentially a Python function that adds additional functionality without any code changes in the original function, in the scenario: Insert log, performance test, transaction processing, permission check, and other applications. Adhere to the closed open principle, the source code modification closed, on the outside of the function to increase openness.

Demand:

In the e-commerce platform, we can see that on different pages, such as the selection of goods, shopping carts, financial payments and other pages can be logged on and can remember the status of the landing, landing once will not need to re-landing on other pages. Use the decorator to pull the landing out.

    1. Add file read/write to determine user name password
    2. When users choose different pages to log in, back to different results

Analysis:

    1. Create a login flag (LOGIN_FALG) to determine if you have logged in
    2. Commodity commodity () Financial Finance ( ) shopping cart Shopp_cart ( ) is three independent functions
    3. Use the parameter decorator to reverse the different results
    4. User chooses to test

Code:

#login flag To determine if you have logged inLogin_falg =True#get the user name password in the fileWith open ('Jd_username_password','R', encoding='UTF8') as F:str= F.read (). Split (',')#with reference adorner for determining user type logindeflogin_type (type):deflogin (f):#Modifying global variables        GlobalLogin_falgdefType_login ():GlobalLogin_falgifLogin_falg:#user Input user name passworduser_name = input ('user_name:'). Strip () User_password= Input ('User_password:'). Strip ()#determine the type of user                ifType = ='JD':                    #determine if the user name password is correct                    ifUser_name = = Str[0] andUser_password = = Str[1]:                        Print('Landing Success! ') F () Login_falg=FalseElse:                        Print('User name password is wrong, please re-enter! ')                elifType = ='Weixin':                    Pass                elifType = ='Amazan':                    Pass            Else:                Print('user has logged in! ')        returnType_loginreturnLogin#Product@login_type ('JD')defcommodity ():Print('Product Features')    Pass#Financial@login_type ('Weixin')defFinance ():Print('financial Functions')    Pass#Shopping Cart@login_type ('Amazan')defShopp_cart ():Print('Shopping Cart Features')    Pass#user Select Type to test while(True): User_type= Input ('Please select type: \n1:[commodity]\n2:[financial Payment]\n3:[Shopping cart]'). Strip ()ifUser_type = ='1': Commodity ()elifUser_type = ='2': Finance ()elifUser_type = ='3': Shopp_cart ()Else:        Print('input Illegal, please re-enter! ')

Python function _ Adorner

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.