020-内嵌函数和闭包python

https://blog.youkuaiyun.com/qq_41556318/article/details/84138388

作业

0、使用global,就可以在函数中修改全局变量

>>> count = 5
>>> def MyFun():
                global count
                count = 10
                print(count)

>>> MyFun()
10
>>> count
10
1、在嵌套函数中,可以通过nonlocal来定义一个变量,允许在内部修改外部函数的局部变量

def fun1():
    x=5
    def fun2():
        nonlocal x
        #声明他不是一个局部变量,就可以进行下面的更改
        x*=x
        return x
    return fun2()
fun1()

返回25

2、不可以在外部直接调用内部函数

def outside():
    print('I am outside!')
    def inside():
        print('I am inside!')

inside()

使用嵌套函数要注意一点就是作用域问题,inside() 函数是内嵌在 outside() 函数中的,所以 inside() 是人妻,除了身为老公的 outside() 可以碰(调用),在外边或者别的函数体里是无法对其进行调用的。

可以修改成:

def outside():
    print('I am outside!')
    def inside():
        print('I am inside!')
        
    inside()
outside()

这样在outside内部调用了inside,然后我们调用outside

3、因为在内部函数中也有一个叫做var的变量,所以python为了保护变量的作用域,将外部的var给屏蔽起来了,所以在内部直接调用是会出现问题的

A.

def outside():
    var = 5
    def inside():
        var = 3
        print(var)
        
    inside()
outside()

B.

def outside():
    var = 5
    def inside():
        var = 3
        print(var)
        
    inside()
outside()

代码A可以,代码B不可以。但是我们修改为代码C

C.

def outside():
    var = 5
    def inside():
        nonlocal var
        print(var)
        var = 3
        
    inside()
outside()

使用nonlocal定义变量

4、

def funOut():
    def funIn():
        print('宾果!你成功访问到我啦!')
    return funIn()

通过直接调用funOut()就可以直接访问funIn()

5.但是如果程序是这样的

def funOut():
    def funIn():
        print('宾果!你成功访问到我啦!')
    return funIn

需要funOut()()才可以访问到

6、预测下面程序会打印什么?

def funX():
    x = 5
    def funY():
        nonlocal x
        x += 1
        return x
    return funY
 
a = funX()
print(a())
print(a())
print(a())

会打印6 7 8

1. 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:

    a) 每位密码为单个小写字母
    b) 每位密码的左右两边均有且只有三个大写字母

逐个遍历判断统计

str1 = '''ABSaDKSbRIHcRHGcdDIF'''
 
countA = 0  # 统计前边的大写字母
countB = 0  # 统计小写字母
countC = 0  # 统计后边的大写字母
length = len(str1)
 
for i in range(length):
    if str1[i] == '\n':
        continue
 
    """
    |如果str1[i]是大写字母:
    |-- 如果已经出现小写字母:
    |-- -- 统计后边的大写字母
    |-- 如果未出现小写字母:
    |-- -- 清空后边大写字母的统计
    |-- -- 统计前边的大写字母
    """
    if str1[i].isupper():
        if countB:
            countC += 1
        else:
            countC = 0
            countA += 1
 
    """
    |如果str1[i]是小写字母:
    |-- 如果小写字母前边不是三个大写字母(不符合条件):
    |-- -- 清空所有记录,重新统计
    |-- 如果小写字母前边是三个大写字母(符合条件):
    |-- -- 如果已经存在小写字母:
    |-- -- -- 清空所有记录,重新统计(出现两个小写字母)
    |-- -- 如果该小写字母是唯一的:
    |-- -- -- countB记录出现小写字母,准备开始统计countC
    """
    if str1[i].islower():
        if countA != 3:
            countA = 0
            countB = 0
            countC = 0
        else: 
            if countB:
                countA = 0
                countB = 0
                countC = 0
            else:
                countB = 1
                countC = 0
                target = i
 
    """
    |如果前边和后边都是三个大写字母:
    |-- 如果后边第四个字母也是大写字母(不符合条件):
    |-- -- 清空记录B和C,重新统计
    |-- 如果后边仅有三个大写字母(符合所有条件):
    |-- -- 打印结果,并清空所有记录,进入下一轮统计
    """
    if countA == 3 and countC == 3:
        if i+1 != length and str1[i+1].isupper():
            countB = 0
            countC = 0
        else:
            print(str1[target], end='')
            countA = 3
            countB = 0
            countC = 0

闭包可以理解为内部函数的一种,直接调用外部函数,得到的结果不是一个具体的数值,而是一个函数,一个有内部变量的函数

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值