python------函數嵌套及做用域鏈

1.三元運算
if條件成立的結果 if 條件 else 條件不成立的結果
例如:
  a=20
  b=10
  c=a if a>b else b
  print(c)
html

2.命名空間 python

  • 全局命名空間:建立的存儲「變量名與值的關係」的空間叫作全局命名空間
  • 局部命名空間:在函數的運行中開闢的臨時的空間叫作局部命名空間
  • 內置命名空間:內置命名空間中存放了python解釋器爲咱們提供的名字:input,print,str,list,tuple...它們都是咱們熟悉的,拿過來就能夠用的方法。

三種命名空間之間的加載順序和取值順序:
  加載順序:內置(程序運行前加載)-->全局(從上到下順序加載進來的)-->局部(調用的時候加載)--->內置
  取值:在局部調用:局部命名空間--->全局命名空間--->內置命名空間
站在全局範圍找:全局----內置----局部
使用:
        全局不能使用局部的
      局部的能夠使用全局的
網絡

3.做用域:就是做用範圍
      1.命名空間和做用域是分不開的
  2.做用域分爲兩種:
           全局做用域:全局命名空間與內置命名空間的名字都屬於全局範圍
          在整個文件的任意位置都能被引用,全局有效
           局部做用域:局部命名空間,只能在局部範圍內生效
3.站在全局看:
    使用名字的時候:若是全局有,用全局的
    若是全局沒有,用內置的
  4.爲何要有做用域?
    爲了函數內的變量不會影響到全局
閉包

  5.globals方法:查看全局做用域的名字【print(globals())】 ide

   locals方法:查看局部做用域的名字【print(locals())】函數

 1 def func():
 2     a = 12
 3     b = 20
 4     print(locals())
 5     print(globals())
 6 
 7 func()
 8 
 9 
在局部調用locals()和globals() View Code

  站在全局看,globals is locals
 global關鍵字:強制轉換爲全局變量
url

 1 # x=1
 2 # def foo():
 3 #     global  x  #強制轉換x爲全局變量
 4 #     x=10000000000
 5 #     print(x)
 6 # foo()
 7 # print(x)
 8 # 這個方法儘可能能少用就少用
 9 
 10 
global View Code

nonlocal讓內部函數中的變量在上一層函數中生效,外部必須有 spa

 1 # x=1
 2 # def f1():
 3 #     x=2
 4 #     def f2():
 5 #         # x=3
 6 #         def f3():
 7 #             # global x#修改全局的
 8 #             nonlocal x#修改局部的(當用nonlocal時,修改x=3爲x=100000000,當x=3不存在時,修改x=2爲100000000 )
 9 #                    # 必須在函數內部
 10 #             x=10000000000
 11 #         f3()
 12 #         print('f2內的打印',x)
 13 #     f2()
 14 #     print('f1內的打印', x)
 15 # f1()
 16 # # print(x)
 17 
 18 
nonlocal View Code

4.函數的嵌套定義 3d

 1 def animal():
 2      def tiger():
 3          print('nark')
 4          print('eat')
 5      tiger()
 6  animal()
View Code

5.做用域鏈 code

 1 x=1
 2 def heihei():
 3   x='h'
 4   def inner():
 5     x='il'
 6     def inner2():
 7       print(x)
 8     inner2()
 9   inner()
 10 heihei()
 11 
 12 
函數的做用域鏈 View Code

6.函數名的本質:就是函數的內存地址

 1 def func():
 2   print('func')
 3 print(func)#指向了函數的內存地址
View Code

7.函數名能夠用作函數的參數

 1 def func():
 2     print('func')
 3 
 4 def func2(f):
 5     f()
 6     print('func2')
 7 func2(func)
 8 
 9 
函數名能夠用做參數 View Code

函數名能夠做爲函數的返回值

 1 return說明1
 2 def func():
 3     def func2():
 4         print('func2')
 5     return func2
 6 f2=func()
 7 f2()
 8 #func2=func()
 9 #func2()
 10 
 11 
 12 2.
 13 
 14 def f1(x):
 15     print(x)
 16     return '123'
 17 
 18 def f2():
 19     ret = f1('s')  #f2調用f1函數
 20     print(ret)
 21 f2()
 22 
 23 
 24 3.
 25 def func():
 26     def func2():
 27         return 'a'
 28     return func2   #函數名做爲返回值
 29 
 30 func2=func()
 31 print(func2())
函數名能夠用做返回值 View Code
 

8.閉包:
閉包:1.閉 :內部的函數
   2.包 :包含了對外部函數做用域中變量的引用
def hei():
  x=20
  def inner():
    x=10 #若是x定義了,他就用本身的了,就實現不了閉包
print(x)

 1 # 閉包的經常使用形式:
 2 def hei():
 3   x=20
 4   def inner():
 5     '''閉包函數'''
 6     print(x)
 7 return inner()
 8 
 9 
閉包函數的常見形式 View Code

判斷閉包函數的方法:__closure__

 1 #輸出的__closure__有cell元素 :是閉包函數
 2 def func():
 3     name = 'eva'
 4     def inner():
 5         print(name)
 6     print(inner.__closure__)
 7     return inner
 8 
 9 f = func()
 10 f()
 11 
 12 
 13 #輸出的__closure__爲None :不是閉包函數
 14 name = 'egon'
 15 def func2():
 16     def inner():
 17         print(name)
 18     print(inner.__closure__)
 19     return inner
 20 
 21 f2 = func2()
 22 f2()
View Code

閉包獲取網絡應用

 1 # from urllib.request import urlopen
 2 # def index(url):
 3 #     def inner():
 4 #         return  urlopen(url).read()
 5 #     return inner
 6 # u='http://www.cnblogs.com/Eva-J/articles/7156261.html#_label1'
 7 # get = index(u)
 8 # print(get())
View Code

9.總結
做用域:小範圍的能夠用大範圍的,可是大範圍的不能用小範圍的
範圍從大到小(圖)

若是在小範圍內,若是要用一個變量,是當前這個小範圍有的,就用本身的
若是在小範圍內沒有,就用上一級的,上一級沒有的,就用上上級的,以此類推
若是都沒有,報錯

10.思惟導圖

 

歸類 :  Python相關

相關文章
相關標籤/搜索