Product Manager Learn Python: How to transfer parameters

Source: Internet
Author: User

This is the 5th article on Python, which focuses on the way in which parameters are passed and how you can design your own functions.

A

This article mainly introduces 2 kinds of parameter transfer methods.

Position parameters

When a function is called, arguments are passed according to the position of the parameter defined by the function.

1 def Right_triangle_area (A, b): 2     return 1/2*a*b34print(Right_triangle_area (3,4))5  #  positional parameter passing

For right Triangle area, A and B are two right-angled edges, where functions are called using positional parameter passing. In positional parameter passing, the order of the parameters is immutable.

Keyword parameter passing

When a function is called, it is specified in the form of "key = value". You can make the function clearer and easier to use, regardless of the order of the parameters.

1 def Right_triangle_area (A, b): 2     return 1/2*a*b34print(Right_triangle_area (b=4,a=3)) 5 # keyword parameter passing

There are some types are the default parameters and variable parameters, etc., at present I do not use, do not do a detailed sharing, interested in Baidu can be self.

Two

To design your own functions

Before describing the method of string and how to create a function, this article integrates the previous learning to design a simple filter for sensitive words.

1. The incoming parameter name (filename) and MSG (information content) can be written on the desktop to write the file name and the contents of the function Text_create, if the desktop does not have this can be written to the file, will create a re-write.

1 deftext_create (name,msg):2     #Create a file, write a message3Desktop_path ='/users/duwangdan/desktop/'4     #Desktop Path5Full_path = Desktop_path + name +'. txt'6     #Desktop path + filename + file suffix7File = Open (Full_path,'W')8     #The ' W ' parameter refers to the write9 file.write (msg)Ten     #writing information in a file One file.close () A     #closing files after writing

In the previous article, "Product Manager Learning Python: Learning to create and invoke functions," it is mentioned that the function needs to return the results after defining the functions. In Python, return is optional, no return can be directly defined and called smoothly, and when not written, the return value is ' None '.

The first part of the sensitive word filter is now complete.

2. Define a function named Text_filter, pass in parameter Word,cencored_word (sensitive word) and changed_word (substitute word), cencored_word default given ' Awesome ', with Changed_ Word default null value to replace, implement sensitive word filtering.

1 def text_filter (word,censored_word='Awesome', change_word='):  2     #  text filter function 3     return  word.replace (censored_ Word,change_word)4     #  replace the sensitive word with the replace () method

3. Define a function named Censored_text_create, pass in the parameter name (file name), MSG (information), use the 2nd function Text_filter, the incoming MSG is filtered and stored in clean_msg, The incoming name and filtered clean_msg are passed into the Text_create function as parameters, and the Censored_text_create function is called to get the filtered text.

1 defcensored_text_create (name,msg):2     #Create a text function after deleting a sensitive word3Clean_msg =text_filter (msg)4     #filter out the sensitive words in msg5 text_create (name,clean_msg)6     #Pass in name and clean_msg to Text_create function7 8Censored_text_create ('Test','Math is awesome!')9 #calling Functions

After completing the above three steps, we can get the text filter of our own design.

The complete code is as follows:

1 deftext_create (name,msg):2Desktop_path ='/users/duwangdan/desktop/'3Full_path = Desktop_path + name +'. txt'4File = Open (Full_path,'W')5 file.write (msg)6 file.close ()7 8 9 defText_filter (word,censored_word='Awesome', change_word="'):Ten     returnword.replace (Censored_word,change_word) One  A  - defcensored_text_create (name,msg): -Clean_msg =text_filter (msg) the text_create (name,clean_msg) -  -Censored_text_create ('Test','Math is awesome!')

Operating environment: Python version, 3.6;pycharm version, 2016.2; PC: Mac

-----End-----

Du Wangdan, public number: Du Wangdan, Internet Product Manager.

Product Manager Learn Python: How to transfer parameters

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.