Day-4: Python function

Source: Internet
Author: User

Each specific problem has specific implementation methods, and for the same class of problems, different concrete methods can be abstracted into this kind of method, when given a specific input, this kind of method will become applicable to a specific problem of the specific method.

This is what the function wants to do, simplifying the abstraction of a class of methods so that each time it is possible to implement the function that you want to implement.

Python has many functions built into it, such as:

ABS (), the function of calculating absolute value;

CMP (x, y), a function that compares the size of x, Y, etc.

You can also define your own functions as you want:

As follows:

def my_abs (x):     if x >= 0:        return  x    Else:        return -X

The definition function begins with a DEF statement, writes out the function name, parentheses, the arguments in parentheses, and a colon, and : then writes the function body in the indent block, returning the function's return value with a return statement.

The function does nothing, it is an empty function, as follows:

def NOP ():     Pass

In fact, the empty function, pass can be used as a placeholder, such as now do not think how to write the function code, you can put one pass , so that the code can run.

When designing parameters for a function, be aware that the required parameters are before the default parameters. The default parameters can simplify the invocation of the function:

def Power (x, n=2):    = 1     while n > 0:        = n-1        = S * x    return s

After you define a power function that has a default power of 2, when you enter only one parameter, the default power is 2, and two parameters are entered, the second argument is the new power number:

>>> Power (5)25>>> Power (5, 2)25

  But notice that the default argument here is immutable, not a variable, or an error occurs when repeated calls occur.

def add_end (l=[]):    l.append ('end')    return L

The first time you use the default parameter call, the result is still right

>>> add_end () ['end']

But multiple invocations make a mistake.

>>> add_end () ['end'end']> >> add_end () ['end'end'  END']

This is because the default parameter in the original function [], which is essentially a list variable , will change with each use. This represents an empty list constant , which should be "L=none"

If the number of inputs to the function is indeterminate, the variable parameter *args is used, and if the passed parameter is a dict type, the keyword argument should be used **kw

The former is to encapsulate multiple parameters into a tuple, which is encapsulated into a dict. So the former has to pass in a single quantity, the latter has to pass in the Key-value key value pair.

When you want to use the required parameters, default parameters, variable parameters, and keyword parameters together, you should be aware that the order in which the parameters are defined is:

Required parameters, default parameters, variable parameters, keyword parameters.

Each time a function call in a recursive function adds a stack frame, the stack is reduced by one layer whenever the function returns, so that the recursive function can overflow the stack.

The method of resolving recursive call stack overflow is optimized by tail recursion . Tail recursion means that when a function returns, it calls itself, and the return statement cannot contain an expression. In this way, the compiler or interpreter can optimize the tail recursion, so that the recursive itself, regardless of the number of calls, only occupy a stack frame, there is no stack overflow situation.

Unfortunately, most programming languages are not optimized for tail recursion, and the Python interpreter does not optimize = =

Day-4: Python function

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.