7

Python__19--函数调用的参数传递与变量的作用域

 1 year ago
source link: https://blog.51cto.com/husheng/5982206
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Table of Contents

1 函数调用的参数传递

形参(形式参数): 在函数定义的时候用到的参数没有具体值,只是一个占位的符号,成为形参; 实参(实际参数): 在调用函数的时候输入的值。

实际参数和形式参数的参数名可以不同

1.1 参数传递的内存分析

Python__19--函数调用的参数传递与变量的作用域_参数
  1. 不可变对象,在函数体内的修改不会影响实参的值
  2. 可变对象,在函数体内的修改会影响实参的值

1.2 函数的返回值

  • 如果函数没有返回值,return可以省略

  • 如果函数返回值是1个,直接返回类型

  • 如果返回值是多个,返回结果为元组

    def fun():
        return'[1,2,3]','hello','world'
    print(fun())
    # 输出:([1,2,3]','hello','world')
    

1.3 函数的参数定义

Python__19--函数调用的参数传递与变量的作用域_参数_02

1.3.1 形式参数、定义

  1. 默认值参数

    只有默认值不同的时候才需要将实参传递给形参

    # 只有默认值不同的时候才需要将实参传递给形参
    def fun(a,b=10):    # b称为默认值参数
        print(a,b)
    fun(100)
    fun(20,30)
    # 输出:
    # 100 20
    # 10 30
    
  2. 个数可变的位置参数,结果为一个元组,使用*

    def fun(*args): 
        print(args)
    fun(10)
    fun(10,20,30)
    # 输出:(10,)
    # 输出:(10,20,30)
    
  3. 个数可变的关键字参数,结果为一个字典,使用**

    def fun(**a):
        print(a)
    fun(a=10)
    fun(a=10,b=20,c=30)
    # 输出:{'a':10}
    # 输出:{'a':10,'b':20,'c':30}
    
  4. 关键字形参,使用*

    def fun(a,b,*,c,d):    # 从*之后的参数,在函数调用时,只能采用关键字参数传递
        print('a=',a)
        print('b=',b)
        print('c=',c)
    fun(10,20,c=30,d=40)
    # 输出:
    # a= 10
    # b= 20
    # c= 30
    
  5. 个数可变的位置参数与个数可变的关键字参数同时存在

    def fun1(*args1,**args2):
    def fun2(a,b,*,c,d,**args2):
    def fun3(a,b=20,*args1,**args2):
    

1.3.2 实际参数、调用

  1. 根据位置将实际参数传递给形式参数

    calc(10,20)

  2. 将序列中的每个元素都转换为位置实参

    def fun(a,b,c):      
        print('a=',a)
        print('b=',b)
        print('c=',c)
    fun(10,20,30)
    lst=[11,22,33]
    fun(*lst)
    # 输出:
    # a=10
    # b=20
    # c=30
    # a=11
    # b=22
    # c=33
    
  3. 关键字实参

    根据形参名称将实际参数传递给形式参数

    calc(b=10,a=20)

  4. 将字典中的每个键值对都转换成关键字实参

    def fun(a,b,c):
        print('a=',a)
        print('b=',b)
        print('c=',c)
    fun(10,20,30)
    dic={'a':111,'b':222,'c':333}
    fun(**dic)
    calc(b=10,a=20)
    # a=10
    # b=20
    # c=30
    # a=111
    # b=222
    # c=333
    

1.4 变量的作用域

一般来说,在函数体外定义的变量叫全局变量,而在函数体内定义的变量叫做局部变量,C语言中也称为内部变量,而局部变量只能在这个函数内部使用,全局变量则是可以作用到整个文件(模块)的范围。

1.4.1 局部变量

函数体内进行定义的变量,无法在函数体外使用

def fun():
    global age
    age=20    
    print(age)
fun()
print(age)
# 使用global定义全局变量,必须先调用函数,该变量才能全局使用
# 输出:
# 20
# 20

1.4.2 全局变量

函数内外均可使用的变量

1.5 递归函数

自己调用自己的函数

# 递归通式:if终止条件,else第n项的通式
def fac(n):
    if n==1:                     #终止条件
        return  1
    else:
        return n*fac(n-1)        #递归调用

1.6 斐波那契数列

斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”。 斐波那契数列指的是这样一个数列: 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711…… 它的规律是:这个数列从第 3 项开始,每一项都等于前两项之和。 在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*),显然,斐波那契数列是一个线性递推数列。

#1  1  2  3  5  8
def fib(n):
    if n==1:
        return 1
    elif n==2:
        return 1
    else:
        return fib(n-1)+fib(n-2)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK