"Python" 15, function

Source: Internet
Author: User


First, the function




3, for the function of Python, we need to remember is

1) The default return value of the function is none.

2) Python is a language that is interpreted and executed from top to bottom by line. Therefore, the definition of the function must precede the function being called. A function with the same name, later defined, overrides the previously defined.

3) When the program executes, the function definition will only read the whole function into memory and not execute immediately. The function body is not executed until the function is called.

4) Whether the parameters of a Python function pass a value pass or a reference pass.

function parameter passing is essentially the same as the variable as a whole, except that two variables are parameter A and argument B, respectively. So, after A=b, A has changed, does B value change? This depends on the object content being mutable


Value passing:

When a function is called, a copy of the actual parameter is passed to the function, and the function modifies the parameter without affecting the actual argument

applies to immutable objects (such as int, str,tuples, and so on) when passed as parameters, such as tuples

Reference Delivery :

When a function is called, the address of the actual parameter is passed to the function, and the function modifies the parameter, which affects the actual argument

applies to mutable objects (such as list,dict, instances of classes, and so on) when passed as parameters, such as lists


Shallow copy:(also known as shadow copy)

copies only the parent object, not the inner child object of the object
Deep copy :

copying an object and its child objects

Assignment is a reference pass

In [the]: L1 = [1, 2, 3]in []: L2 = l1in []: l2out[76]: [1, 2, 3]in []: L3 = L1.copy () in []: l3out[78]: [1, 2, 3]in              [+]: ID (L3) # L3 and L1 apply a different memory object out[79]: 140149284135624In [140149295996616In]: ID (L1) out[80]: [Bayi]: ID (L2) # L2 and L1 refer to the same memory object out[81]: 140149295996616


This is important:

def f1 (a_list): s = ID (a_list) a_list[0] = 5 print (s) print (a_list) Li = [1,2,3,4,]f1 (LI) print (ID (LI)) print (LI)

Operation Result:

11797576[5, 2, 3, 4]11797576[5, 2, 3, 4]


Second, the parameters of the function

For a function, the most important point of knowledge is the parameter.

Parameters are divided into formal parameters ( formal arguments) and actual parameters (arguments).

Def f1 (A, B, c): Pass F1 (1, 2, 3)

Among them, A,b,c is the formal parameter, which is the real parameter, that is, the actual parameters to be passed.

usually when we pass the parameter, we pass it by the position of the parameter, which is called " positional parameter ".

and sometimes we pass parameters in the form of parameter name = value , which is called a keyword parameter or a specified parameter .


There are several formal parameters in Python:


1. Position parameters


2. Keyword parameters


3. Default parameters


4. Dynamic Parameters


5. Universal Parameters


Python 15, 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.