2. Python programming advanced 01: function

Table of contents

1. Call the function

2. Define a function

3. Function parameter passing

3.1 Required function parameters

3.2 Function keyword arguments

3.3 Function default parameters

3.4 Function variable length parameters

4. Anonymous functions

5. return statement

6. About function parameters that can be changed and objects that cannot be changed

6.1 Parameter passing of python functions

7. Local variables and global variables

7.1 Local variables

7.2 Global variables

8. Local functions


A function is a piece of code that performs a specific task. By defining a piece of code as a function and assigning a function name to the function, the program can call this piece of code multiple times when needed. Therefore, functions are an important means of code reuse.

In Python, functions are widely used. We have seen many functions before, such as input(), print(), range(), len() functions, etc. These are built-in functions of Python and can be used directly .

In addition to the built-in functions that can be used directly, Python also supports custom functions, that is, a regular and reusable code is defined as a function, so as to achieve the purpose of writing once and calling multiple times. This is called a user-defined function .

Learning functions requires focusing on the definition of functions and the methods of function calls.

1. Call the function

Python has many useful built-in functions that we can call directly.

To call a function, you need to know the name and parameters of the function. For example, the function abs for absolute value has only one parameter. Documentation can be viewed directly from Python's official website:

http://docs.python.org/3/library/functions.html#abs

You can also view the help information of the abs function through help(abs) on the interactive command line.

Example: View the abs function help on the python interactive command line

C:\Desktop\py>python

Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> help(abs)

Help on built-in function abs in module builtins:

abs(x, /)

    Return the absolute value of the argument.

The basic syntax format of a function call is as follows:

function name([parameter value])

When calling the function, if the number of parameters passed in is incorrect, a TypeError error will be reported.

If the number of parameters passed in is correct, but the parameter type cannot be accepted by the function, a TypeError error will also be reported and an error message will be given. Therefore, it should be noted that the number and type of incoming parameters must correspond.

Sample code: Call the abs function to calculate the absolute value:

#!/usr/bin/python3

print(abs(120))

print(abs(-120))

Sample code: Call the int() function to convert string type data into integer data and return:

#!/usr/bin/python3

data=input("Please enter an integer:")

int_data=int(data)

print("Your input integer: %d" % (int_data))

2. Define a function

Simple rules for defining functions:

(1) The function code block begins with the def keyword, followed by the function identifier name and parentheses ().

(2) Any incoming parameters and independent variables must be placed between parentheses, which can be used to define parameters.

(3) The first statement of the function can optionally use the document string—used to store the function description.

(4) The function content starts with a colon and is indented.

(5) , return [expression] ends the function, and optionally returns a value to the caller. return without an expression is equivalent to returning None.

 Python defines functions using the def keyword, and the general format is as follows:

def function name (parameter list):

#A code block consisting of zero to multiple executable statements

[return [return value]]

Among them,  the parts enclosed by [] are optional parts, which can be used or omitted.

By default, parameter values ​​and parameter names are matched in the order defined in the function declaration.

Example: Using a function to output "Hello World!"

#!/usr/bin/python3#Definition function

def hello() :

    print("Hello World!")

#Call functions

hello()

Example: Designing a Sum Function

#!/usr/bin/python3

def my_sum(x,y):

    return x+y

data1=int(input("Please enter integer 1:"))

data2=int(input("Please enter integer 2:"))

#Call functions

data_sum=my_sum(data1,data2)

print("data_sum=%d" % (data_sum))

When the statement inside the function body is executed, once the return is executed, the function is executed and the result is returned. Therefore, very complex logic can be realized through conditional judgment and loops inside the function.

If there is no return statement, the result will be returned after the function is executed, but the result is None, and return None can be abbreviated as return.

3. Function parameter passing

3.1 Required function parameters

函数必要参数,也称位置参数,指的是必须按照正确的顺序将实际参数传到函数中,调用函数时传入实际参数的数量和位置都必须和定义函数时保持一致,否则 Python 解释器会抛出 TypeError 异常,并提示缺少必要的位置参数。

示例代码:

#!/usr/bin/python3

#指定参数的默认值

def cal_max(x, y):

    if(x>y):

       return x

    else:

       return b

a=int(input("the first num:"))

b=int(input("the first num:"))

print(cal_max(a))

结果:

the first num:2

the first num:8

Traceback (most recent call last):

  File "run.py", line 11, in <module>

    print(cal_max(a))

TypeError: cal_max() missing 1 required positional argument: 'y'

3.2 函数关键字参数

前面的例子代码,使用函数时所用的参数都是位置参数,即传入函数的实际参数必须与形式参数的数量和位置对应

Python还支持关键字参数,这样可以避免牢记参数位置的麻烦,令函数的调用和参数传递更加灵活方便

关键字参数是指使用形式参数的名字来确定输入的参数值。通过此方式指定函数实参时,不再需要与形参的位置完全一致只要将参数名写正确即可。

因此,Python 函数的参数名应该具有更好的语义,这样程序可以立刻明确传入函数的每个参数的含义。示例代码:

#!/usr/bin/python3

def print_func(data,str):

    print("你传入的整数:%d" % (data))

    print("你传入的字符串:%s" % (str))

    return

#按照位置传参

print_func(1234,"hello world")

#按照关键字传参

print_func(data=1234,str="hello world")

#按照关键字传参

print_func(str="hello world",data=1234)

#关键字和位置参数混合传参

print_func(1234,str="hello world")

#print_func(str="hello world",1234)  这样传参是错误的,混合传参要按照顺序(有关键字的情况下)

如果希望在调用函数时混合使用关键字参数和位置参数,则关键字参数必须位于位置参数之后,在关键字参数之后的只能是关键字参数。

3.3 函数默认参数

前面说过,在调用函数时,如果传入的参数与函数不匹配或者没有传入参数,解释器会抛出异常。

为了解决这个问题,Python 允许为参数设置默认值即在定义函数时,直接给形式参数指定一个默认值(定义的时候就指定一个默认值),这样的话,即便调用函数时没有给拥有默认值的形参传递参数,该参数可以直接使用定义函数时设置的默认值。

定义带有默认值参数的函数,其语法格式如下:

def 函数名(...形参名=默认值)

代码块

在使用上面格式定义函数时,有默认值的参数必须放在没有默认值参数的最后,否则会产生语法错误。示例代码:

#!/usr/bin/python3

#指定参数的默认值

def print_func(data=666,str="欢迎学习Python编程"):

    print("你传入的整数:%d" % (data))

    print("你传入的字符串:%s" % (str))

    return

#使用默认参数

print_func()

#使用自定义参数

print_func(7890) #传入的参数覆盖默认的参数

#使用自定义参数

print_func(str="哈哈哈") #传入的参数覆盖默认的参数

 Python 中,可以使用“函数名.__defaults__”查看函数的默认值参数的当前值,其返回值是一个元组。示例代码:

#!/usr/bin/python3

#指定参数的默认值

def print_func(data=666,str="欢迎学习Python编程"):

    print("你传入的整数:%d" % (data))

    print("你传入的字符串:%s" % (str))

    return

print(print_func.__defaults__)

'''

#使用默认参数

print_func()

#使用自定义参数

print_func(7890) #传入的参数覆盖默认的参数

#使用自定义参数

print_func(str="哈哈哈") #传入的参数覆盖默认的参数

'''

结果:

(666, '欢迎学习Python编程')

3.4 函数不定长参数

有时候需要一个函数能处理比当初声明时更多的参数,这些参数叫做不定长参数,和上面讲的几种参数不同,声明时不会命名。

Python提供了2种方法实现不定长参数:  

(1)在形参名前加一个*, 表示接受任意多的参数

(2)在形参名前加两个**, 表示接受任意的关键字参数

第一种: 函数参数带一个*号

语法格式如下:

def 函数名([参数列表,*不定长参数):

代码块

加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数。示例代码:

#!/usr/bin/python3

def print_func(str, *args_list):

    print("你传入的参数:",str)

    print("传入的可变长参数:",args_list)

    return

print_func("hello world","python","PHP","C/C++")

print_func(1,2,3,4,5)

结果:

你传入的参数: hello world

传入的可变长参数: ('python', 'PHP', 'C/C++')

你传入的参数: 1

传入的可变长参数: (2, 3, 4, 5)

示例代码:

#!/usr/bin/python3

def print_func(*args_list):

    print("传入的可变长参数:",args_list)

    return

print_func("hello world","python","PHP","C/C++")

print_func(1,2,3,4,5)

结果:

传入的可变长参数: ('hello world', 'python', 'PHP', 'C/C++')

传入的可变长参数: (1, 2, 3, 4, 5)

第二种: 函数参数带两个*号

语法格式如下:

def 函数名([参数列表,**不定长参数):

代码块

加了两个星号 ** 的参数会以字典的形式导入。示例代码:

#!/usr/bin/python3

def print_func(**args_list):

    print("传入的可变长参数:",args_list)#变成键值对(字典)

    return

print_func(a="hello world",b="python",c="PHP",d="C/C++")

结果:

传入的可变长参数: {'a': 'hello world', 'b': 'python', 'c': 'PHP', 'd': 'C/C++'}

4. 匿名函数

python 使用 lambda来创建匿名函数。所谓匿名,就是不再使用 def 语句这样标准的形式定义一个函数。lambda 函数的语法只包含一个语句,格式如下:

lambda [参数1 [,参数1,.....参数n]]:函数代码块

lambda函数的语法说明

(1). lambda 只是一个表达式,函数体比 def 简单很多。

(2). lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。

(3). lambda 函数拥有自己的命名空间,且不能访问自己参数列表之外或全局命名空间里的参数。

虽然lambda函数看起来只能写一行,却不等同于C或C++的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。

匿名函数限制,只能有一个表达式,不用写return,返回值就是该表达式的结果。

用匿名函数不必担心函数名冲突,匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数。示例代码:

#!/usr/bin/python3

# 函数说明

sum = lambda arg1, arg2: arg1 + arg2 #前面是参数列表,后面是语句块

# 调用sum函数

print ("相加后的值为 : ", sum( 10, 20 ))

print ("相加后的值为 : ", sum( 20, 20 ))

示例代码:

#!/usr/bin/python3

#函数说明

print_func = lambda arg1, *arg2: print("传入的参数:",arg1, arg2)

'''

print_func = lambda arg1, *arg2:

    print("传入的参数:",arg1, arg2)

'''

#错误的语法格式:匿名函数的语句块要直接跟在冒号后面

#调用函数

print_func(1,2,3,4,5,6)

结果:

传入的参数: 1 (2, 3, 4, 5, 6)

5. return语句

Python中,用 def 语句创建函数时,可以用 return 语句指定应该返回的值,该返回值可以是任意类型。需要注意的是,return 语句在同一函数中可以出现多次,但只要有一个得到执行,就会直接结束函数的执行。

函数中,使用 return 语句的语法格式如下:

return [返回值]

其中,返回值参数可以指定,也可以省略不写(不写将返回: 空值 None)。

None(N 必须大写)是一个特殊的常量。和 False 不同,它不表示 0,也不表示空字符串,而表示没有值,也就是空值。

None 常用于 assert、判断以及函数无返回值的情况。在前面章节中一直使用 print() 函数输出数据,其实该函数的返回值就是 None。因为它的功能是在屏幕上显示文本,根本不需要返回任何值,所以 print() 就返回 None。

对于所有没有 return 语句的函数定义,Python 都会在末尾加上 return None,使用不带值的 return 语句(也就是只有 return 关键字本身),那么就返回 None。

6. 关于函数形参可更改与不可更改对象

 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象。-->列表和字典是可更改的,其余不可更改。

不可变类型:变量赋值 a=5 后再赋值 a=10,这里实际是新生成一个 int 值对象 10,再让 a 指向它,而 5 被丢弃,不是改变a的值,相当于新生成了a。

可变类型:变量赋值 la=[1,2,3,4] 后再赋值 la[2]=5 则是将 list la 的第三个元素值更改,本身la没有动,只是其内部的一部分值被修改了。

6.1 python 函数的参数传递

不可变类型:类似 c++ 的值传递,如 整数、字符串、元组。如fun(a),传递的只是a的值,没有影响a对象本身。比如在 fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身。

可变类型:类似 c++ 的引用传递,如 列表,字典。如 fun(la),则是将 la 真正的传过去,修改后fun外部的la也会受影响。

python 中一切都是对象,严格意义我们不能说值传递还是引用传递,我们应该说传不可变对象和传可变对象。

示例代码:python 传不可变对象

#!/usr/bin/python3

def func(data):

    data=2

    print("data=",data)

a=100

func(a)

print("a=",a)  #这里打印a的值还是100

结果:

data= 2

a= 100

示例代码: 传递可更改对象

可变对象在函数里修改了参数,那么在调用这个函数的函数里,原始的参数也被改变了

#!/usr/bin/python3

def func(list):

    list[1]="666"   

    print("list=",list)

list_data=["1","2","3","4","5"]

func(list_data) 

#调用函数

print("list_data=",list_data)

结果:

list= ['1', '666', '3', '4', '5']

list_data= ['1', '666', '3', '4', '5']

 

7. 局部变量与全局变量

7.1 局部变量

在程序中定义一个变量时,这个变量是有作用范围的,变量的作用范围被称为它的作用域。

变量的作用域指程序代码能够访问该变量的区域,如果超过该区域,将无法访问该变量。根据定义变量的位置(有效范围),可以将变量分为局部变量和全局变量。

局部变量: 是指在函数内部定义并使用的变量,它只在函数内部有效。

每个函数在执行时,系统都会为该函数分配一块“临时内存空间”,所有的局部变量都被保存在这块临时内存空间内。当函数执行完成后,这块内存空间就被释放,这些局部变量也会释放,因此离开函数之后就不能再访问局部变量,否则解释器会抛出 NameError 错误。示例代码:

#!/usr/bin/python3

def func():

    data=1234

    print("data=",data)

func()

#调用函数

print("data=",data)   #错误: 无法访问局部变量

结果:

Traceback (most recent call last):

  File "run.py", line 8, in <module>

    print("data=",data)   #错误: 无法访问局部变量

NameError: name 'data' is not defined

7.2 全局变量

全局变量: 是指函数内外都可以访问的变量。

即全局变量既可以在各个函数的外部使用,也可以在各函数内部使用。

定义全局变量的方式:

(1)在函数体外定义的变量,一定是全局变量,示例代码:

#!/usr/bin/python3

data=1234

def func():

print("data=",data)

func()

#调用函数

print("data=",data)

结果:

data= 1234

data= 1234

(2)在函数体内使用 global 关键字定义全局变量,示例代码:

#!/usr/bin/python3

def func():   

    global data #使用global关键字修饰变量时,不能赋值

    data=1234

    print("data=",data)

   

func() 

#调用函数

print("data=",data)

结果:

data= 1234

data= 1234

8. 局部函数

Python 支持在函数体内定义函数,这种被放在函数体内定义的函数称为局部函数。在默认情况下,局部函数对外部是隐藏的,局部函数只能在其封闭(enclosing)函数内有效,其封闭函数也可以返回局部函数,以便程序在其他作用域中使用局部函数。

如果封闭函数没有返回局部函数,那么局部函数只能在封闭函数内部调用。

如果封闭函数将局部函数返回,且程序使用变量保存了封闭函数的返回值,那么这些局部函数的作用域就会被扩大,程序可以使用封闭函数的返回值自由地调用局部函数,就像使用全局函数一样。示例代码:

#!/usr/bin/python3

def func():

    def sum(a,b): #定义局部函数

       return a+b

    return sum   #返回局部函数

a=func() #调用全局函数

print("求和:", a(12,45))  #通过全局函数的返回值,调用内部的局部函数

结果:

求和: 57

下一篇:

Guess you like

Origin blog.csdn.net/qq_40088639/article/details/128553032