标签:返回 tab trace cap 好处 eve 生成 字符串 局部变量
def func():
print("呵呵"
print(func)
结果:
<function func at 0x1101e4ea0>
def func():
print("呵呵")
print(func)
a = func # 把函数当成一个变量赋值给另一个变量
a() # 函数调用
func()
结果:
<function func at 0x00000257861A6048>
呵呵
呵呵
def func1():
pass
def func2():
pass
lst = [func1,func2]
for el in lst:
el()
def func1():
print("我是1")
def func2():
print("我是2")
def func3():
print("我是3")
lst = [func1, func2, func3]
for el in lst:
el()
结果:
我是1
我是2
我是3
def fun():
pass
def proxy(fu):
fu()
proxy(func)
def func1():
print("我是func1")
def func2():
print("我是func2")
def func(fn, gn): # 函数名可以作为参数进行传递
print("我是func")
fn()
gn()
print("哈哈哈")
func(func1, func2)
结果:
我是func
我是func1
我是func2
哈哈哈
def func():
def inner():
pass
return inner
func()()
def func_1():
print("这里是函数1")
def func_2():
print("这里是函数2")
print("这里是函数1")
return func_2
# fn = func_1() # 执行函数1,函数1返回的是函数2,这时fn指向的就是上面函数2
# fn() # 执行上面返回的函数
func_1()()
结果:
这里是函数1
这里是函数1
这里是函数2
def func1():
name = "alex"
def func2():
print(name) # 闭包
func2()
func1()
结果:
alex
def func1():
name = "alex"
def func2():
print(name) # 闭包
func2()
print(func2.__closure__) # (<cell at 0x10c2e20a8: str object at 0x10c3fc650>,)
func1()
from urllib.request import urlopen
def outer():
# 常驻内存
s = urlopen("http://www.xiaohua100.cn/index.html").read()
def getContent(): # 闭包
return s
return getContent
print("爬取内容.....")
pa = outer() # 这个时候就开始加载校花100的内容
# 后面需要用到这里面的内容就不需要在执行非常耗时的网络连接操作了了
ret = pa() # 获取内容
print(ret)
ret = pa() # 重新获取内容
print(ret)
ret = pa()
print(ret)
ret = pa()
print(ret)
def outer():
a = 10
def inner():
print(a)
return inner
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()()()
# 对的
s = "abc"
for c in s:
print(c)
结果:
a
b
c
# 错的
for i in 123:
print(i)
结果:
Traceback (most recent call last):
File "F:/python/python kecheng/day12/code/04 闭包.py", line 77, in <module>
for i in 123:
TypeError: ‘int‘ object is not iterable
注意:报错信息里有这么一句话: ‘int‘ object is not iterable ,翻译过来就是整数类型对象是不可迭代的,
iterable表示可迭代的,表示迭代协议,那么如何验证你的数据类型是否符号可迭代协议,我们可以通过dir
函数来实现查看
s = "我的哈哈哈"
print(dir(s)) # 可以打印对象中的方法和函数
print(dir(str)) # 也可以打印类中声明的方法和函数
print(dir(tuple))
print(dir(list))
print(dir(open("护士少妇嫩模.txt"))) # 文件对象
print(dir(set))
print(dir(dict))
结果:
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘count‘, ‘index‘]
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
[‘__and__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__iand__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__ior__‘, ‘__isub__‘, ‘__iter__‘, ‘__ixor__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__or__‘, ‘__rand__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__ror__‘, ‘__rsub__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘, ‘__subclasshook__‘, ‘__xor__‘, ‘add‘, ‘clear‘, ‘copy‘, ‘difference‘, ‘difference_update‘, ‘discard‘, ‘intersection‘, ‘intersection_update‘, ‘isdisjoint‘, ‘issubset‘, ‘issuperset‘, ‘pop‘, ‘remove‘, ‘symmetric_difference‘, ‘symmetric_difference_update‘, ‘union‘, ‘update‘]
[‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘clear‘, ‘copy‘, ‘fromkeys‘, ‘get‘, ‘items‘, ‘keys‘, ‘pop‘, ‘popitem‘, ‘setdefault‘, ‘update‘, ‘values‘]
通过dir函数发现,这几个可以进行for循环的东西都有__iter__函数,包括range也有
s = "岁月枯荣仿佛春秋一梦"
it = s.__iter__() # 获取迭代器
print(dir(it)) # 迭代器里有__iter__ 还有__next__
结果:
[‘__class__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__length_hint__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__next__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__setstate__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘]
lst = ["赵四","刘能", "赵本山", "宋小宝"]
it = lst.__iter__()
from collections import Iterable # 可迭代对象
from collections import Iterator # 迭代器
print(isinstance(lst, Iterable)) #True
print(isinstance(lst, Iterator)) #False
print(isinstance(it, Iterable)) #True
print(isinstance(it, Iterator)) #True
lst = ["暖春","炎夏","悲秋","寒冬"]
it = lst.__iter__()
# 偏方
print("__iter__" in dir(it)) #True
print("__next__" in dir(it)) #True
# 可以通过dir来判断数据是否是可迭代的, 以及数据是否是迭代器
s = "岁月枯荣仿佛春秋一梦"
it = s.__iter__() # 获取迭代器
# print(dir(it)) # 迭代器里有__iter__ 还有__next__
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
语法:
it = xx.__iter__()
while 1:
try:
data = it.__next__()
xxxxxx
except StopIteration:
break
# 迭代器模拟for循环
lst = ["娃哈哈", "乳娃娃", "爽歪歪", "小洋人","果粒橙"]
# for el in lst: # 底层用的是迭代器
# print(el)
it = lst.__iter__() # 获取迭代器
while 1:
try: # 尝试执行
el = it.__next__() # 获取下一个元素
print(el)
except StopIteration: # 处理错误
break
使?用while循环+迭代器来模拟for循环(必须要掌握)
标签:返回 tab trace cap 好处 eve 生成 字符串 局部变量
原文地址:https://www.cnblogs.com/rixian/p/9456509.html