"Concise Python Tutorial" Learning Notes "functions"

Source: Internet
Author: User
Tags uppercase letter

Defining Functions :The function is defined by the DEF keyword. The DEF keyword is followed by the identifier name of a function followed by a pair of parentheses. Parentheses can include some variable names, which end with a colon. Next is a piece of statement, which is the function body.
function Formal Parameters: The parameter name in the function is a formal parameter and the value you provide to the function call is called an argument.
Local Variables: When you declare variables within a function definition, they are not related to other variables with the same name outside the function, that is, the variable name is local to the function. This is called the scope of the variable. All variables are scoped to their defined blocks, starting with the point where their names are defined.
Global Statement:The global statement is used to declare that X is global-so when we assign a value to x within a function, the change is reflected when we use the value of x in the main block. You can use the same global statement to specify multiple global variables. For example, Global x, Y, Z.
Default parameter values: The default parameter value should be immutable. "Important" only in end of formal parameter list those parameters can have default parameter values, that is, you cannot declare a formal parameter with a default value and then declare a parameter that has no default value when declaring a function parameter.
This is because the value assigned to the parameter is assigned according to the position. For example, Def func (A, b=5) is valid, but def func (a=5, b) is not valid.
Key Parameters: If you have a function with a number of parameters and you want to specify only a subset of them, you can assign values by naming them-this is called a key parameter-we use the name (the keyword) instead of the location (the method we have been using previously) to assign the argument to the function. There are two advantages to this--one, because we don't have to worry about the order of the parameters, it's easier to use the function. Second, assuming that the other parameters have default values, we can only assign values to the parameters we want.
Return Statement: Note that the return statement without the returned value is equivalent to return none. None is a special type of Python that represents nothing. For example, if a variable has a value of none, you can indicate that it has no value. Unless you provide your own return statement, each function contains a return none statement at the end. By running print someFunction (), you can see that the function someFunction does not use the return statement as if: Def someFunction ():     Pass

The Pass statement represents an empty block of statements in Python.


docstrings:

The Convention for a document string is a multiline string whose first line begins with an uppercase letter and ends with a period. The second line is a blank line, and the third line starts with a detailed description. It is strongly recommended that you follow this convention when using a document string in your function.

You can use __doc__(note double underline) to call the function's document string property (which is the name of the function). Keep in mind that Python takes everything as an object, including this function. We'll learn more about objects in the later class chapter.

If you've already used help () in Python, you've seen the use of docstings! All it does is grab the __doc__ property of the function and show it neatly to you. You can try this on the function above-just include help (Printmax) in your program. Remember to press Q to exit help.

Automation tools can also extract documents from your program in the same way. Therefore, I strongly recommend that you write a document string for any formal function that you write. Use docstrings similar to help () with the pydoc command that came with your Python distribution.

"Concise Python Tutorial" Learning Notes "functions"

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.