python中函数的嵌套和作用域链

1.三元运算
if条件成立的结果 if 条件 else 条件不成立的结果
例如:
  a=20
  b=10
  c=a if a>b else b
  print(c)

2.命名空间

  •   全局命名空间:创建的存储“变量名与值的关系”的空间叫做全局命名空间
  •    局部命名空间:在函数的运行中开辟的临时的空间叫做局部命名空间
  •    内置命名空间:内置命名空间中存放了python解释器为我们提供的名字:input,print,str,list,tuple...它们都是我们熟悉的,拿过来就可以用的方法。

三种命名空间之间的加载顺序和取值顺序:
加载顺序:内置(程序运行前加载)-->全局(从上到下顺序加载进来的)-->局部(调用的时候加载)--->内置
取值:在局部调用:局部命名空间--->全局命名空间--->内置命名空间
站在全局范围找:全局----内置----局部
使用:
  全局不能使用局部的
   局部的可以使用全局的

3.作用域:就是作用范围
  1.命名空间和作用域是分不开的
  2.作用域分为两种:
       全局作用域:全局命名空间与内置命名空间的名字都属于全局范围
          在整个文件的任意位置都能被引用,全局有效
    局部作用域:局部命名空间,只能在局部范围内生效   
  3.站在全局看:
    使用名字的时候:如果全局有,用全局的
    如果全局没有,用内置的
  4.为什么要有作用域?
    为了函数内的变量不会影响到全局

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

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

def func():
a = 12
b = 20
print(locals())
print(globals())

func()

在局部调用locals()和globals()

站在全局看,globals is locals
   global关键字:强制转换为全局变量

# x=1
# def foo():
# global x #强制转换x为全局变量
# x=10000000000
# print(x)
# foo()
# print(x)
# 这个方法尽量能少用就少用

global

nonlocal让内部函数中的变量在上一层函数中生效,外部必须有

# x=1
# def f1():
# x=2
# def f2():
# # x=3
# def f3():
# # global x#修改全局的
# nonlocal x#修改局部的(当用nonlocal时,修改x=3为x=100000000,当x=3不存在时,修改x=2为100000000 )
# # 必须在函数内部
# x=10000000000
# f3()
# print('f2内的打印',x)
# f2()
# print('f1内的打印', x)
# f1()
# # print(x)

nonlocalv

4.函数的嵌套定义

def animal():

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

5.作用域链 

x=1
def heihei():
  x='h'
  def inner():
    x='il'
    def inner2():
      print(x)
    inner2()
  inner()
heihei()

函数的作用域链

6.函数名的本质:就是函数的内存地址

def func():
2   print('func')
3 print(func)#指向了函数的内存地址

7.函数名可以用做函数的参数

def func():
print('func')

def func2(f):
f()
print('func2')
func2(func)

函数名可以用作参数

函数名可以作为函数的返回值

复制代码
return说明1
def func(): def func2(): print('func2') return func2 f2=func() f2() #func2=func() #func2()


2.
def f1(x):
print(x)
return '123'

def f2():
ret = f1('s') #f2调用f1函数
print(ret)
f2()


3.
def func():
def func2():
return 'a'
return func2 #函数名作为返回值

func2=func()
print(func2())




复制代码

8.闭包:
闭包:1.闭 :内部的函数 
   2.包 :包含了对外部函数作用域中变量的引用 
def hei(): 
  x=20 
  def inner(): 
    x=10 #如果x定义了,他就用自己的了,就实现不了闭包 
print(x)

# 闭包的常用形式:
def hei():
  x=20
  def inner():
    '''闭包函数'''
    print(x)
return inner()

闭包函数的常见形式

判断闭包函数的方法:__closure__

#输出的__closure__有cell元素 :是闭包函数
def func():
name = 'eva'
def inner():
print(name)
print(inner.__closure__)
return inner

f = func()
f()


#输出的__closure__为None :不是闭包函数
name = 'egon'
def func2():
def inner():
print(name)
print(inner.__closure__)
return inner

f2 = func2()
f2()

闭包获取网络应用

# from urllib.request import urlopen
# def index(url):
# def inner():
# return urlopen(url).read()
# return inner
# u='http://www.cnblogs.com/Eva-J/articles/7156261.html#_label1'
# get = index(u)
# print(get())

9.总结
作用域:小范围的可以用大范围的,但是大范围的不能用小范围的
范围从大到小(图)

如果在小范围内,如果要用一个变量,是当前这个小范围有的,就用自己的
如果在小范围内没有,就用上一级的,上一级没有的,就用上上级的,以此类推
如果都没有,报错

10.思维导图

       



猜你喜欢

转载自www.cnblogs.com/intruder/p/10928771.html
今日推荐