1. 程式人生 > >python-第一類物件,閉包,迭代器

python-第一類物件,閉包,迭代器

# def fn():
#     print("我叫fn")

# fn()
# print(fn)  # <function fn at 0x0000000001D12E18>
# fn()
# gn = fn # 函式名可以進行賦值
# print(gn)
# gn()

# fn = 666
# print(fn) # 666


# def func1():
#     print("朱祁鎮")
#
# def func2():
#     print("徐階")
#
# def func3():
#     print("王陽明")
#
# def func4():
#     print("魏忠賢")
# # lst = [func1, func2, func3, func4] # 函式+() 就是呼叫. # print(lst) # # # lst[0]() # # for el in lst: # el是列表中的每一項. # # el() # 拿到函式. 執行函式 # # # a = 10 # b = 20 # c = 30 # lst = [a, b, c] # print(lst) # def wrapper(): # def inner(): # print("我的天, 還可以扎樣寫") # print(inner) # <function wrapper.<locals>.inner at 0x00000000028989D8>
# inner() # return inner # # ret = wrapper() # <function wrapper.<locals>.inner at 0x00000000028989D8> # print(ret) # ret() # def wrapper(): # def inner(): # print("哈哈哈") # return inner # 函式名可以像返回值一樣返回 # # ret = wrapper() # ret() # 在函式外面訪問了函式內部的函式 # ret() # ret() # # def func1():
# a = 10 # return a # print(func1()) # 函式可以作為引數進行傳遞 def func1(): print("謝晉") def func2(): print('楊士奇') def func3(): print('徐渭') def func4(): print("柳如是") # 代理. 裝飾器的雛形 def proxy(a): # a就是變數. 形參 print("我是代理") a() print("代理執行完畢")
# 閉包的優點:
#   1, 可以保護變數不被其他人侵害
#   2, 保持一個變數常駐記憶體

# def wrapper():
#     a = "哈哈" # 不安全的一種寫法
#     name = "周杰倫"
#     def inner():
#         print(name) # 在內層函式中使用了外層函式的區域性變數
#         print(a)
#     def ok():
#         nonlocal a
#         a = 108
#         print(a)
#     return inner  # 返回函式名
#
# ret = wrapper()
# ret()
#
# def ok():
#     global a
#     a = 20
#     print(a )



# def wrapper():
#     name = "周杰倫" # 區域性變數常駐與記憶體
#     def inner():
#         print(name) # 在內層函式中使用了外層函式的區域性變數
#     return inner  # 返回函式名
#     # inner()
#
# ret = wrapper() # ret是一個內層函式
# ret() # ret是inner, 執行的時機是不確定的, 必須保證裡面的name必須存在


# 超級簡易版爬蟲
# from urllib.request import urlopen # 匯入一個模組
# # 幹掉數字簽名
# import ssl
# ssl._create_default_https_context = ssl._create_unverified_context
#
#
# def func():
#     # 獲取到網頁中的內容, 當網速很慢的時候. 反覆的去開啟這個網站. 很慢
#     content = urlopen("https://www.dytt8.net/").read()
#
#     def inner():
#         return content.decode("gbk") # 網頁內容
#     return inner
#
# print("開始網路請求")
# ret = func() # 網路請求已經完畢
# print("網路請求完畢")
# print("第一次", ret()[5])
# print("第二次", ret()[5]) #



def wrapper():
    name = "alex"
    def inner():
        print("胡辣湯")
    print(inner.__closure__) # 檢視是否是閉包. 有內容就是閉包, 沒有內容就不是閉包
    inner()

wrapper()
 
  
  
# s = "今天下午考試. 你們還要睡覺嗎?"
#
# for c in s: # 字串是一個可迭代物件
#     print(c)

# for c in "哼哼哈哈":
#     print(c)
#
# for i in 123: # 'int' object is not iterable
#     print(i)


# dir() 可以幫我們檢視xxx資料能夠執行的操作
# print(dir(str)) # __iter__
# print(dir(int)) # 沒有__iter__
# print(dir(list)) # 有__iter__
# print(dir(dict)) # 有__iter__
# print(dir(bool)) # 沒有__iter__
# for i in True: # 'bool' object is not iterable
#     print(i)

# print(dir(range))
# f = open("呵呵", mode="r", encoding="utf-8")
# print(dir(f))

# 共性:所有帶有__iter__的東西都可以進行for迴圈, 帶有__iter__的東西就是可迭代物件

# lst = ["賈樟柯", "李安", "楊德昌", "王家衛"]
#
# # 1. 只能向下執行, 不能反覆
# # 2. 結束的時候會給我們扔出來一個錯誤 StopIteration
# # 3. 整合所有的資料型別進行遍歷(int,bool除外)
#
# # print("__iter__" in dir(lst))
# it = lst.__iter__()  # 拿到的是迭代器 <list_iterator object at 0x0000000001DCC160>
#
# print(it.__next__()) # 下一個
# print(it.__next__()) # 下一個
# print(it.__next__()) # 下一個
# print(it.__next__()) # 下一個
# # print(it.__next__()) # 下一個 # StopIteration 停止迭代
# # 想回去
# it = lst.__iter__() # 只能重新獲取迭代器

# s = {"張無忌", "賈樟柯", "寶寶", "風扇哥", "門神"}
# it = s.__iter__()
# print(it.__next__())
# print(it.__next__())
# print(it.__next__())
# print(it.__next__())
# print(it.__next__())


# lst = ["海爾兄弟", "阿童木", "葫蘆娃", "舒克貝塔", "大風車"]
# # 模擬for迴圈 for el in lst:
# it = lst.__iter__() # 獲取到迭代器0
# while 1: # 迴圈
#     try: # 嘗試
#         el = it.__next__() # 那資料
#         print(el)
#     except StopIteration: # 出了錯誤, 意味著資料拿完了
#         break # 結束迴圈


# 官方通過程式碼判斷是否是迭代器
# 藉助於兩個模組 Iterator迭代器, Iterable可迭代的
# from collections import Iterable, Iterator
#
# lst = [1,2,3]
# # print(lst.__next__())
#
# print(isinstance(lst, Iterable)) # xxx是否是xxx型別的. True
# print(isinstance(lst, Iterator)) # False
#
# it = lst.__iter__()  # 迭代器一定可迭代,  可迭代的東西不一定是迭代器
#
# print(isinstance(it, Iterable)) # xxx是否是xxx型別的. True
# print(isinstance(it, Iterator)) # True
#
# for el in it:
#     print(el)

 



proxy(func1)
proxy(func3)
proxy(func4)