美文网首页
Python函数,变量,闭包

Python函数,变量,闭包

作者: o0龙与猫0o | 来源:发表于2020-02-10 16:09 被阅读0次

Python中函数可以设定默认值。
Python是dynamically typed的,可以接收任何数据类型,函数的参数也是,不用考虑输入的数据类型,而是将其交给具体的代码去判断执行,但是缺点是,必须在函数的开头加上数据的类型检查。

函数的嵌套

Python的函数支持嵌套

def f1():
    print('hello')
    def f2():
        print('world')
    f2()
f1()
 
# 输出
hello
world

函数嵌套的作用:

  • 能够保证内部函数的隐私,内部函数只能被外部函数访问,不会暴露全局作用域。比如一些隐私数据,数据库的用户名和密码等。
def connect_DB():
    def get_DB_configuration():
        ...
        return host, username, password
    conn = connector.connect(get_DB_configuration())
    return conn
  • 合理的使用函数嵌套,能够提高程序的运行效率,可以看下递归的python代码,求一个数的阶乘:
def factorial(input):
    # validation check
    if not isinstance(input, int):
        raise Exception('input must be an integer.')
    if input < 0:
        raise Exception('input must be greater or equal to 0' )
    ...
 
    def inner_factorial(input):
        if input <= 1:
            return 1
        return input * inner_factorial(input-1)
    return inner_factorial(input)
 
 
print(factorial(5))

使用函数嵌套,输入是否合法的检查就只需要一次,如果不使用函数嵌套,就需要在inner_factorial中继续做参数检查

变量

  • 注意:函数内部不能随意改变全局变量的值
MIN_VALUE = 1
MAX_VALUE = 10
def validation_check(value):
    ...
    MIN_VALUE += 1
    ...
validation_check(5)

MIN_VALUE和MAX_VALUE都是全局变量,这样会报错

UnboundLocalError: local variable 'MIN_VALUE' referenced before assignment

因为,Python的解释器会 默认函数内部的变量 是局部变量,但是发现局部变量MIN_VALUE并没有声明,所以无法执行相关操作。如果一定要在函数内部改变全局变量的值,必须加上 global 声明:

MIN_VALUE = 1
MAX_VALUE = 10
def validation_check(value):
    global MIN_VALUE
    ...
    MIN_VALUE += 1
    ...
validation_check(5)

如果遇到函数内部局部变量和全局变量同名的情况,在函数内部,局部变量会覆盖全局的变量。

  • 嵌套函数,内部函数可以访问外部函数定义的变量,但是无法修改,若要修改,必须加上 nonlocal 这个关键字
def outer():
    x = "local"
    def inner():
        nonlocal x # nonlocal 关键字表示这里的 x 就是外部函数 outer 定义的变量 x
        x = 'nonlocal'
        print("inner:", x)
    inner()
    print("outer:", x)
outer()
# 输出
inner: nonlocal
outer: nonlocal

闭包(closure)

闭包和嵌套函数类似,只是闭包外部函数的返回值是内部函数

def nth_power(exponent):
    def exponent_of(base):
        return base ** exponent

    return exponent_of  # 返回值是 exponent_of 函数


square = nth_power(2)  # 计算一个数的平方
cube = nth_power(3)  # 计算一个数的立方

print(square)
print(cube)

print(square(2))
print(cube(2))

<function nth_power.<locals>.exponent_of at 0x000002599895B5E0>
<function nth_power.<locals>.exponent_of at 0x000002599895B550>
4
8

使用闭包的一个原因是,闭包能让程序变得更加简洁易读,上面的逻辑其实可以直接写成

def nth_power_rewrite(base, exponent):
    return base ** exponent

但是,调用时区别就出来了

# 不适用闭包
res1 = nth_power_rewrite(base1, 2)
res2 = nth_power_rewrite(base2, 2)
res3 = nth_power_rewrite(base3, 2)
...
 
# 使用闭包
square = nth_power(2)
res1 = square(base1)
res2 = square(base2)
res3 = square(base3)

相关文章

  • 2018-11-20

    python函数的闭包 闭包: 嵌套函数调用外部函数的变量 注意: 闭包必须是内部函数调用外部函数定义的变量,这其...

  • Python 入门之 闭包

    Python 入门之 闭包 1、闭包 (1)在嵌套函数内使用(非本层变量)和非全局变量就是闭包 (2)_ clos...

  • Python装饰器与闭包!

    闭包是Python装饰器的基础。要理解闭包,先要了解Python中的变量作用域规则。 变量作用域规则 首先,在函数...

  • python函数之闭包

    目录 python函数之闭包什么是闭包python中的namespace闭包的条件闭包的优点 python函数之闭...

  • (9) python之闭包

    闭包闭包 = 函数 + 环境变量(函数定义的时候) 一个最简单的闭包 闭包不受外部变量影响 非闭包 闭包 闭包 只...

  • Swift中的闭包定义

    定义闭包变量 定义可选闭包变量 定义闭包别名 定义闭包常量 在函数内部 作为函数参数 作为函数参数(隐式参数类型)...

  • 2019-01-05 闭包

    内部函数和外部函数中被内部函数调用的变量组成了闭包。 例子:函数line与变量a构成了闭包。 1.闭包似优化了变量...

  • python基础-08-内置函数、作用域、闭包、递归

    python基础-内置函数、作用域、闭包、递归 1.常见的内置函数 2.函数内变量的作用域 3.内嵌函数和闭包 4...

  • 闭包和立即执行函数

    闭包闭包:内部的函数引用了外部的函数的变量,则产生闭包。闭包的作用:把变量封装在函数中,只有内部函数可以用,让函数...

  • Python的闭包和装饰器

    什么是python闭包? 函数a中包含了函数b因为函数b能够访问自己作用域以外的变量n,所以称之为闭包。 在此基础...

网友评论

      本文标题:Python函数,变量,闭包

      本文链接:https://www.haomeiwen.com/subject/hnpaxhtx.html