函數是第一類對象,即函數能夠當作數據傳遞python
能夠被引用閉包
能夠當作參數傳遞函數
返回值能夠是函數ui
能夠當作容器類型的元素code
def foo(): print('from foo') def index(): print('from index') dic = { 'foo':foo, 'index':index, } while True: choice = input(">>>>>").strip() if choice in dic: dic[choice]()
def max(x,y): return x if x > y else y def max4(a,b,c,d): res1 = max(a,b) res2 = max(res1,c) res3 = max(res2,d) return res3 print(max4(234,456,123,789))
def f1(): def f2(): def f3(): print("from f3") f3() f2() f1() # 返回值 from f3 ,即 f3的值
名稱空間:存放名字的地方
名稱空間分爲三種orm
隨着python解釋器的啓動而產生對象
a = [1,2,3,4,5] print(max(a))
文件的執行會產生全局名稱空間,指的是文件級別定義的名字都會放入改空間ip
x = 1 def fun(): x = 2 print(x) fun() print(x)
調用函數時會產生局部名稱空間,只在函數調用時臨時綁定,調用結束解綁定ci
x = 10000 def func(): x = 1 def f1(): print(x) def f2(): print(x) f2() f1() func()
做用域即範圍(做用域關係是在函數定義階段就已經固定的,與函數的調用位置無關)
查看做用域:globals(),locals()作用域
def f1(): x = 1 y = 2 def f2(): print(x,y) return f2 f = f1() print(f.__closure__[0]) print(f.__closure__[0].cell_contents)
def foo(): print('一') yield 1 print('二') yield 2 print('三') yield 3 print('四') g = foo() # for i in g: # print(i) print(next(g)) print(next(g)) print(next(g)) print(next(g))
- | - | Built-in Functions | - | - |
---|---|---|---|---|
abs() | dict() | help() | min() | stator() |
all() | dir() | hex() | next() | slice() |
any() | divmod() | id() | object() | sorted() |
ascii() | enumerate() | input() | oct() | staticmethod() |
bin() | enav() | int() | open() | str() |
bool() | exec() | isinstance() | ord() | sun() |
bytearray() | filter() | issubclass() | pow() | super() |
bytes() | float() | iter() | print() | tuple() |
callable() | format() | len() | property() | type() |
chr() | frozenset() | list() | range() | vars() |
classmethod() | getattr() | locals() | repr() | zip() |
compile() | globals() | map() | reversed() | __import__() |
complex() | hasattr() | max() | round() | - |
delattr() | hash() | memoryview() | set() | - |