云南省昆明市邮编,用姓名查身份证号码,东方网贷导航
def func(): print("呵呵") print(func)
结果: <function func at 0x1101e4ea0>
def func(): print("呵呵") print(func)
a = func # 把函数当成⼀个变量赋值给另⼀个变量 a() # 函数调⽤ func()
def func1(): print("呵呵") def func2(): print("呵呵") def func3(): print("呵呵") def func4(): print("呵呵") lst = [func1, func2, func3] for i in lst: i()
def func(): print("吃了么") def func2(fn): print("我是func2") fn() # 执⾏传递过来的fn print("我是func2")
func2(func) # 把函数func当成参数传递给func2的参数fn.
def func_1(): print("这⾥是函数1") def func_2(): print("这⾥是函数2") print("这⾥是函数1") return func_2
fn = func_1() # 执⾏函数1. 函数1返回的是函数2, 这时fn指向的就是上⾯函数2 fn() # 执⾏上⾯返回的函数
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 at0x10c3fc650>,) 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/").read() def get_content(): return content return get_content fn = but() # 这个时候就开始加载校花100的内容 # 后⾯需要⽤到这⾥⾯的内容就不需要在执⾏⾮常耗时的⽹络连接操作了 content = fn() # 获取内容 print(content)
content2 = fn() # 重新获取内容 print(content2)
# 对的 s = "abc" for c in s: print(c) # 错的 for i in 123: print(i)
结果: traceback (most recent call last): file "/users/sylar/pycharmprojects/oldboy/iterator.py", line 8, in <module> for i in 123: typeerror: 'int' object is not iterable
s = "我的哈哈哈" print(dir(s)) # 可以打印对象中的⽅法和函数 print(dir(str)) # 也可以打印类中声明的⽅法和函数
['__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']
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__', '__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'] ['_chunk_size', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkclosed', '_checkreadable', '_checkseekable', '_checkwritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines'] ['__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']
l = [1,2,3] l_iter = l.__iter__() from collections import iterable from collections import iterator print(isinstance(l,iterable)) #true print(isinstance(l,iterator)) #false print(isinstance(l_iter,iterator)) #true print(isinstance(l_iter,iterable)) #true
s = "我爱北京天安⻔" c = s.__iter__() # 获取迭代器 print(c.__next__()) # 使⽤迭代器进⾏迭代. 获取⼀个元素 我 print(c.__next__()) # 爱 print(c.__next__()) # 北 print(c.__next__()) # 京 print(c.__next__()) # 天 print(c.__next__()) # 安 print(c.__next__()) # ⻔ print(c.__next__()) # stopiteration
for i in [1,2,3]: print(i)
lst = [1,2,3] lst_iter = lst.__iter__() while true: try: i = lst_iter.__next__() print(i) except stopiteration: break
1. 节省内存2. 惰性机制3. 不能反复, 只能向下执⾏
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论