函数的命名空间

著名的python之禅

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

命名空间的分类:(1)全局命名空间(2)局部命名空间(3)内置命名空间

三种命名空间的加载与取值顺序

加载顺序:内置命名空间(程序运行前加载)->全局命名空间(程序运行中:从上到下加载)->局部命名空间(程序运行中:调用时才加载)

取值时分为:局部调用和全局调用

局部调用:局部命名空间->全局命名空间->内置命名空间

x = 1
def f(x):
print(x) print(10)

全局调用:全局命名空间->内置命名空间

x = 1
def f(x):
print(x) f(10)
print(x)

函数的作用域:按照生效范围可以分为全局作用域和局部作用域。

通过global和local可以更改函数的作用域。

函数的嵌套与作用域:

函数的嵌套

def f1():
def f2():
def f3():
print("in f3")
print("in f2")
f3()
print("in f1")
f2() f1()

函数的作用域

def f1():
a=1
def f2():
a=2
def f3():
a=3
print("a in f3:",a) print("a in f2:",a)
f3() print("a in f1:",a)
f2() f1()
def f1():
a = 1
def f2():
a = 2
print('a in f2 : ', a)
f2()
print('a in f1 : ',a) f1()

nonlocal关键字(不是本地的)

使用要求:

1.外部必须有这个变量
2.在内部函数声明nonlocal变量之前不能再出现同名变量
3.内部修改这个变量如果想在外部有这个变量的第一层函数中生效
def f1():
a = 1
def f2():
nonlocal a
a = 2
f2()
print('a in f1 : ',a) f1()

函数名的本质是函数的内存地址:它可以被引用、被当作容器类型的元素、当作函数的参数和返回值。

闭包函数:

常用方式

def func():
name = 'eva'
def inner():
print(name)
return inner f = func()
f()

用途:我们在外部函数调用内部函数时

闭包函数的嵌套:

def wrapper():
money = 1000
def func():
name = 'eva'
def inner():
print(name,money)
return inner
return func f = wrapper()
i=f()
print(i)
i()

嵌套了几个就要加括号执行几个

闭包函数的应用:

from urllib.request import urlopen

def index():
url = "http://www.xiaohua100.cn/index.html"
def get():
return urlopen(url).read()
return get xiaohua = index()
content = xiaohua()
print(content)

最新文章

  1. Git从入门到学会
  2. Alpha阶段第三次Scrum Meeting
  3. 开启Java博客
  4. 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
  5. ural 1142. Relations
  6. python3中urllib2的问题
  7. madown标签说明
  8. T-SQL实例 函数结果设置为列别名
  9. Android开发_字符串处理类-TextUtils类
  10. Lua I/0输入输出
  11. C语言库函数大全及应用实例九
  12. linux挂载数据盘步骤
  13. head语法
  14. maven构建myeclipse 工程
  15. ELK使用1-Elasticsearch使用
  16. 使用Nexus2搭建Maven本地仓库
  17. web中的集群与分布式
  18. python opencv3 FLANN单应性匹配
  19. 前端学习笔记之CSS后代选择器、子元素选择器、相邻兄弟选择器区别与详解
  20. 用CIFilter生成QRCode二维码图片

热门文章

  1. 利用OpenCV给图像添加中文标注
  2. Thinkphp table doesn't exist
  3. Spring 与 SpringMVC (或许不完整)
  4. openEntityForm如何给关于(regardingobjectid)类型查找字段赋值?
  5. CentOS 7上VNCServer的安装使用
  6. JMeter 线程组之ConcurrencyThreadGroup介绍
  7. tsung HTTP协议统计报告分析
  8. springboot 学习之路 4(日志输出)
  9. 机器学习之隐马尔科夫模型HMM(六)
  10. LeetCode题解之Clone Graph