亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Function value transfer problem

Let’s look at an example first:

# -*- coding: UTF-8 -*-
def chagne_number( b ):
    b = 1000
b = 1
chagne_number(b)
print( b )

The final output result is:

1

Some people may have questions here, why is the value of b not changed through the function chagne_number? Why is there no change and the output result is still 1? This problem is discussed in many programming languages, and the explanation of the principle is similar.

This is mainly about the transfer of function parameters, which are type objects. The basic data types in Python were also introduced before. These type objects can be divided into changeable types and unchangeable types

In Python, strings, integers, floating point types, and tuples are unchangeable objects, while list, dict, etc. can be changed. object.

For example:

Unchangeable type: Variable assignment a = 1 actually generates an integer object 1, and then the variable a points to 1. When a = 1000, in fact That is to generate another integer object 1000, and then change the point of a, no longer pointing to integer object 1, but to 1000, and finally 1 will be discarded

Changeable type: variable assignment a = [1,2,3,4,5,6] is to generate an object list. There are 6 elements in the list, and the variable a points to the list. a[2] = 5 is to change the third element of list a. The element value change here is different from the above. It does not redirect a, but directly modifies the element value in the list.

ca71dacdee25f4d37deb9d0275fb3ba.png

This will also affect the passing of parameters in the function:

Unchangeable type: similar to c value passing, Such as integers, strings, and tuples. For example, fun(a) only transfers the value of a and does not affect the a object itself. For example, modifying the value of a inside fun(a) only modifies another copied object and does not affect a itself.

Changeable types: c-like reference passing, such as lists and dictionaries. For example, fun (a), a is actually passed to it. After modification, a outside fun will also be affected.

Therefore, in the first example, b = 1, an integer object 1 is created. , the variable b points to this object, and then when passing the function chagne_number, the variable b is copied by value. What is passed is only the value of b, and does not affect b itself. For details, you can look at the modified example and have a better understanding through the printed results.

# -*- coding: UTF-8 -*-
def chagne_number( b ):
    print('函數(shù)中一開始 b 的值:{}' .format( b ) )
    b = 1000
    print('函數(shù)中 b 賦值后的值:{}' .format( b ) )
b = 1
chagne_number( b )
print( '最后輸出 b 的值:{}' .format( b )  )

Printed result:

函數(shù)中一開始 b 的值:1
函數(shù)中 b 賦值后的值:1000
最后輸出 b 的值:1

Of course, if the parameter is a changeable type, then after calling this function, the original value will also be changed. The specific example is as follows:

# -*- coding: UTF-8 -*-
def chagne_list( b ):
    print('函數(shù)中一開始 b 的值:{}' .format( b ) )
    b.append(1000)
    print('函數(shù)中 b 賦值后的值:{}' .format( b ) )
b = [1,2,3,4,5]
chagne_list( b )
print( '最后輸出 b 的值:{}' .format( b )  )

Output result:

函數(shù)中一開始 b 的值:[1, 2, 3, 4, 5]
函數(shù)中 b 賦值后的值:[1, 2, 3, 4, 5, 1000]
最后輸出 b 的值:[1, 2, 3, 4, 5, 1000]
Continuing Learning
||
submitReset Code