python-fullstack-s13-day11-python??????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-fullstack-s13-day11-python??????相关的知识,希望对你有一定的参考价值。

???????????????   ??????   ??????   get   closure   FN   www   ??????   ?????????   

????????????????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????
1???????????????????????????

1 def func():
2     print("??????")
3 print(func) # function func at 0x02969C90

2???????????????????????????????????????

def func():
    print("??????")
print(func)
a = func # ???????????????????????????????????????????????????
a()    # ???????????? func()

3??????????????????????????????????????????

 1 def func1():
 2     print("haha1")
 3 def func2():
 4     print("haha2")
 5 def func3():
 6     print("haha3")
 7 def func4():
 8     print("haha4")
 9 lst = [func1,func2,func3,func4] # ???????????????????????????????????????
10 for i in lst:
11     i()

4???????????????????????????????????????

def func():
    print("?????????")
def func2(fn):
    print("??????func2")
    fn() # ?????????????????????fn
    print("??????func2")
func2(func)    # ?????????func??????????????????func2?????????fn

5??????????????????????????????????????????

def func1():
    print("???????????????1")
    def func2():
        print("???????????????2")
    print("???????????????1")
    return func2
fn = func1() # ????????????1?????????1??????????????????2?????????fn??????????????????????????????2
fn()    # ???????????????????????????

????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????

def func1():
    name = "alex"
    def func2():
    ??????print(name) # ??????
    func2()
func1()

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????


??????????????????__closure__????????????????????????????????????????????? __closure__??????cell?????????????????????None???????????????

def func1():
    name = "alex"
    def func2():
        print(name) # ??????
    func2()
    print(func2.__closure__) # (<cell at 0x03634B90: str object at 0x03424980>,)
func1()  

??????, ?????????????????????????????????????????

def outer():
    name = "alex"
    # ????????????
    def inner():
        print(name)
    return inner
fn = outer() # ??????????????????, ????????????????????????????????????
fn() # ??????????????????

# ????????????????????????? ?????????, ????????????????????????????????????????????

def func1():
    def func2():
        def func3():
            print("??????")
        return func3
    return func2
func1()()()

????????????????????????????????????????????????

from urllib.request import urlopen
def but():
    content = urlopen("http://www.xiaohua100.cn/index.html").read()
    def get_content():
        return content
    return get_content
fn = but() # ?????????????????????????????????100?????????
# ???????????????????????????????????????????????????????????????????????????????
content = fn() # ????????????
print(content)
content2 = fn() # ??????????????????
print(content2)

?????????????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????????????

def create_people():
    print("??????????????????????????????????????????????????????")
# create_people()

def water(fn):
    def inner():
        print("?????????")
        fn()
        print("??????")
    return inner
# func = water(create_people)
# func()
create_people = water(create_people) # ?????????????????????????????????????????????????????????
create_people()

?????????????????????

@water
def create_people():
    print("??????????????????????????????????????????????????????")
# create_people()

def water(fn):
    def inner():
        print("?????????")
        fn()
        print("??????")
    return inner
# func = water(create_people)
# func()
# create_people = water(create_people) # ?????????????????????????????????????????????????????????
create_people()

???????????????????????????????????????????????????????????????????????????????????????

????????????????????????????????????????????????def/def inner ????????????





以上是关于python-fullstack-s13-day11-python??????的主要内容,如果未能解决你的问题,请参考以下文章

python-fullstack-s13-day04-python基础

python-fullstack-s13-day09-python基础

python-fullstack-s13-day03-python基础

python-fullstack-s13-day02-python基础

python-fullstack-s13-day24-python模块部分

python-fullstack-s13-day07-python基础