Python全栈(第一期)Day10

今日主要内容:
函数命名空间和作用域
函数的嵌套使用
闭包的基本定义与方法

一,作业讲解

1,写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

#方法一:
def func(l):
    return l[1::2]  #切片
print(func([1,2,3,4,5]))


#方法二:
def odd(s):
    list1 = []
    for i in s:
        if i % 2 == 0:
            pass
        else:
            list1.append(s[i])
    return list1


ss = odd([1, 2, 3, 4, 5, 6])
print(ss)

输出结果:
[2, 4]
[2, 4, 6]

2,写函数,计算传入字符串中【数字】、【字母】、【空格】 以及 【其他】的个数,并返回结果。

def func(s):
    dic = {'num': 0, 'alpha': 0, 'space': 0, 'other': 0}
    for i in s:
        if i.isdigit():
            dic['num'] += 1
        elif i.isalpha():
            dic['alpha'] += 1
        elif i.isspace():
            dic['space'] += 1
        else:
            dic['other'] += 1
    return dic
s = input('请输入:')
print(func(s))

输出结果:
请输入:hello python **
{‘num’: 0, ‘alpha’: 11, ‘space’: 2, ‘other’: 2}

3,写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容,并返回结果。

def func(x):
    if type(x) is str and x:  #参数是字符串
        for i in x:
            if i == ' ':
                return True
    elif x and type(x) is list or type(x) is tuple: #参数是列表或者元组
        for i in x:
            if not i:
                return True
    elif not x:
        return True

print(func([1,2,50]))
print(func('hello python'))
print(func([1, 2, []]))
print(not[])
'''
s = ''
if not s:
    print('python')

#明确not的使用方法。

'''

输出结果:
None
True
True
True

4,求两个数的最大值

‘’’
三元运算: 返回两个数值的最大值
变量 = 条件返回True的结果 if 条件 else 条件返回False的结果
必须要有结果
必须要有if和else
只能是简单的情况
‘’’

a = 1
b = 5
c = a if a > b else b   #三元运算
print(c)



'''
def fun(a, b):
    if a > b:
        return a
    else:
        return b


print(fun(8, 9))
'''

5,写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成整个文件的批量修改操作(进阶)。

def func(filename, old, new):
    with open(filename, encoding='utf-8') as f, open('%s.bak' % filename, 'w', encoding='utf-8') as f2:
        for line in f:
            if old in line:
                line = line.replace(old, new)

            f2.write(line)

    import os
    os.remove(filename)  # 删除文件
    os.rename('%s.bak' % filename, filename)  # 重命名文件

二,函数的命名空间

1,命名空间知识点

# 内置命名空间 —— python解释器
#     print()
#     input()
#     list
#     tuple
#
#     就是python解释器一启动就可以使用的名字存储在内置命名空间中
#     内置的名字在启动解释器的时候被加载进内存里
# 全局命名空间 —— 我们写的代码但不是函数中的代码
#     是在程序从上到下被执行的过程中依次加载进内存的
#     放置了我们设置的所有变量名和函数名
# 局部命名空间 —— 函数
#     就是函数内部定义的名字
#     当调用函数的时候 才会产生这个名称空间 随着函数执行的结束 这个命名空间就又消失了

在局部:可以使用全局、内置命名空间中的名字
在全局:可以使用内置命名空间中的名字,但是不能用局部中使用
在内置:不能使用局部和全局的名字的

在正常情况下,直接使用内置的名字
当我们在全局定义了和内置名字空间中同名的名字时,会使用全局的名字
当我自己有的时候 我就不找我的上级要了
如果自己没有 就找上一级要 上一级没有再找上一级 如果内置的名字空间都没有 就报错
多个函数应该拥有多个独立的局部名字空间,不互相共享

#case1
a = 10
def fun():
    print(a)

fun()
print(a)

print('python')

#case2

b = 10
def fun():
    b = 5
    print(b)

fun()
print(b)

输出结果:
10
10
python
5
10

上级顺序:
内置>全局>局部
倒置原则

#case:并没有去执行python内置函数中的input
def input():
    print('in input now')
a = input()
print(a)

输出结果:
in input now
None

2,接着补充一点知识

func --> 函数的内存地址
下面这两种调用方式均可
1,函数名()
2,函数的内存地址()

作用域:

全局作用域 —— 作用在全局 —— 内置和全局名字空间中的名字都属于全局作用域 ——globals()
局部作用域 —— 作用在局部 —— 函数(局部名字空间中的名字属于局部作用域) ——locals()

a = 1
def func():
    global a
    a = 2

func()
print(a)

输出结果:
2

Note:
1,对于不可变数据类型 在局部可是查看全局作用域中的变量,但是不能直接修改。
2,如果想要修改,需要在程序的一开始添加global声明。
3,如果在一个局部(函数)内声明了一个global变量,那么这个变量在局部的所有操作将对全局的变量有效。

a = 1
b = 2
def func():
    x = 'aaa'
    y = 'bbb'
    print(locals())
    print(globals())

func()
print(globals())
print(locals()) #本地的  放在这里打印的和全局的一模一样

#globals 永远打印全局的名字
#locals 输出什么 根据locals所在的位置

输出结果:
{‘y’: ‘bbb’, ‘x’: ‘aaa’}
{‘name’: ‘main’, ‘doc’: ‘\n命名空间 有三种\n\n’, ‘package’: None, ‘loader’: <_frozen_importlib_external.SourceFileLoader object at 0x000001B6695F2EB8>, ‘spec’: None, ‘annotations’: {}, ‘builtins’: <module ‘builtins’ (built-in)>, ‘file’: ‘E:/百度云下载/Python全栈9期(第一部分):基础+模块+面向对象+网络编程/day10/day10课堂笔记/5.函数的命名空间.py’, ‘cached’: None, ‘a’: 1, ‘b’: 2, ‘func’: <function func at 0x000001B6679E2EA0>}
{‘name’: ‘main’, ‘doc’: ‘\n命名空间 有三种\n\n’, ‘package’: None, ‘loader’: <_frozen_importlib_external.SourceFileLoader object at 0x000001B6695F2EB8>, ‘spec’: None, ‘annotations’: {}, ‘builtins’: <module ‘builtins’ (built-in)>, ‘file’: ‘E:/百度云下载/Python全栈9期(第一部分):基础+模块+面向对象+网络编程/day10/day10课堂笔记/5.函数的命名空间.py’, ‘cached’: None, ‘a’: 1, ‘b’: 2, ‘func’: <function func at 0x000001B6679E2EA0>}
{‘name’: ‘main’, ‘doc’: ‘\n命名空间 有三种\n\n’, ‘package’: None, ‘loader’: <_frozen_importlib_external.SourceFileLoader object at 0x000001B6695F2EB8>, ‘spec’: None, ‘annotations’: {}, ‘builtins’: <module ‘builtins’ (built-in)>, ‘file’: ‘E:/百度云下载/Python全栈9期(第一部分):基础+模块+面向对象+网络编程/day10/day10课堂笔记/5.函数的命名空间.py’, ‘cached’: None, ‘a’: 1, ‘b’: 2, ‘func’: <function func at 0x000001B6679E2EA0>}

三,函数的嵌套和作用域链

1,case

def max(a,b):
    return a if a>b else b

def the_max(x,y,z):  #函数的嵌套调用
    c = max(x, y)
    return max(c, z)

print(the_max(1,2,3))

输出结果:
3

2,嵌套中的作用域

a = 5
def outer():
    a = 1
    def inner():
        a = 2
        def inner2():
            nonlocal a
            #声明了一个上面第一层局部变量(离当前函数最近的有a的)
            #这里并不能用global,因为此时a并不是全局变量
            a += 1      #不可变数据类型的修改
        inner2()
        print('改变了inner中变量-->', 'a:', a)
    inner()
    print('未改变outer中变量-->', 'a:', a)


outer()
print('未改变全局中变量-->', 'a:', a)

输出结果:
改变了inner中变量–> a: 3
未改变outer中变量–> a: 1
未改变全局中变量–> a: 5

Note:
1,nonlocal 只能用于局部变量 找上层中离当前函数最近一层的局部变量。
2,声明了nonlocal的内部函数的变量修改会影响到 离当前函数最近一层的局部变量。
3, 对全局无效对局部也无效,只是对最近的一层有影响。

3,再补充一点小知识

#note:对计算机而言,并没有变量名的概念,只有内存地址


def func():
    print(123)

# func()  #函数名就是内存地址
func2 = func  #函数名可以赋值
func2()

输出结果:
123

四,闭包

1,闭包的基本定义

#闭包:嵌套函数,内部函数调用外部函数的变量

def outer():
    a = 1
    def inner():
        print(a)
    return inner   #闭包的 常用方式

inn = outer()
inn()
#闭包的一个好处就是:保护了变量a

2,怎么验证是不是闭包

def outer():
    a = 1
    def inner():
        print(a)
    return inner   

print(outer().__closure__)

输出结果:
(<cell at 0x0000017B52449BE8: int object at 0x000000005A086C10>,)
Note:
若有cell,则证明该函数是闭包。

3,网页

# import urllib  #模块  关于URL请求



#method1
from urllib.request import urlopen   #可以打开网页
# ret = urlopen('https://www.duba.com/?f=edge').read()
# print(ret)





#method2
#函数方法:
#缺点:每次调用 get_url ,都会生成一次url。需要进一步优化
def get_url():
    url = 'https://www.duba.com/?f=edge'
    ret = urlopen(url).read()
    print(ret)

get_url()










#method3
def get_url():
    url = 'https://www.duba.com/?f=edge'
    def get():
        ret = urlopen(url).read()
        print(ret)
    return get

get_func = get_url()
get_func()

猜你喜欢

转载自blog.csdn.net/qq_42615032/article/details/84348853