python2 中没有nonlocal。

函数名是什么?

    函数名就是函数的名字, 本质:变量,特殊的变量。

1.单独打印函数名:

def func():
print(666)
print(func) # <function func at 0x0000020C8C7C8F28> 函数的存储位置。

2.函数名的赋值:

def func():
print(666)
f = func
print(f) #<function func at 0x0000015710A08F28>

3.函数名可以作为参数:

a = 1
def f(x):
print(x)
f(a) #

4.函数名可以作为容器类数据的元素。

def func1():
print(1111)
def func2():
print(2222)
def func3():
print(3333)
# func1()
# func2()
# func3()
l = [func1,func2,func3]
for i in l:
i()
#
#
# def func1():
print(1111)
def func2():
print(2222)
def func3():
print(3333)
l = []
for i in range(1,4):
l.append('func'+str(i))
for i in l:
eval(i)() # i 里面是什么类型 eval(i) 就可以直接把i提出来。

5.函数名可以作为函数的返回值。

def func(x):
return x
ret = func(5)
print(ret) #

闭包:就是内层函数对外层函数(非全局)变量的引用。

def wraaper():
def inner():
print(666)
return inner
ret = wraaper() # inner
ret() # inner()

如何判断是否为闭包:内层函数名.__closure__    如果有cell 就是闭包。

def wrapper():
name = '老男孩'
def inner():
print(name)
inner()
print(inner.__closure__)
wrapper() # 老男孩
# (<cell at 0x000001473F9A0B58: str object at 0x000001474033A5D0>,)

    如果不是闭包就打印None.

name = '顾清秋'
def wraaper():
def inner():
print(name)
inner()
print(inner.__closure__)
wraaper() # 顾清秋
# None

面试题:‘闭包’

闭包:当函数开始执行时,如果遇到了闭包,他有一个机制,他会永远开辟一个内存空间,将闭包中的变量等值放入其中,不会随着函数的执行完毕而消失。

name = '老男孩'
def wraaper(n):
# 相当于 n = '老男孩'
def inner():
print(n) # 老男孩
inner()
print(inner.__closure__) # (<cell at 0x0000017B8CCD0B58: str object at 0x0000017B8D63A5D0>,)
wraaper(name)

第一类对象(first - class object)指

1. 可在运行期创建。

2. 可用作函数参数或返回值。

3. 可存入变量的实体。

闭包:

  闭包函数:

        内部函数包含对外部作用域而非全局作用域变量的的引用,该内部函数成为闭包函数。

        ##  函数内部定义的函数被称为内部函数。

闭包函数获取网络应用:

from urllib.request import urlopen
def index():
url = "http://www.xiaohua100.cn/index.html"
def get():
return urlopen(url).read().decode('utf-8')
return get
xiaohua = index()
content = xiaohua()
print(content)

   

什么是装饰器?

  装饰器本质上就是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象。

  装饰器的应用场景:比如插入日志,性能测试,事务处理,缓存等等场景。

装饰器的形成过程:

  装饰器---简单版

import time
def func1():
print('in func1')
def timer(func): # func = func1
def inner():
start = time.time()
func()
time.sleep(0.3)
print(time.time()-start)
return inner
func1 = timer(func1) #inner
func1() # inner()

  装饰器---语法糖

import time
def timer(func):
def inner():
start_time = time.time()
func()
time.sleep(0.3)
end_time = time.time()
print(end_time-start_time)
return inner
@timer #语法糖 # func1 = timer(func1)
def func1():
print("in func1")
func1()

  带参数的装饰器:

import time
def timer(func):
def inner(a):
start_time = time.time()
func(a)
time.sleep(0.3)
end_time = time.time()
return inner
@timer
def func1(a):
print(a) #
func1(1)

hold 住所有参数的装饰器

import time
def timer(func):
def inner(*args,**kwargs):
start_time = time.time()
ret = func(*args,**kwargs)
time.sleep(0.3)
end_time = time.time()
print(end_time-start_time)
return ret
return inner
@timer
def func1(a,b):
print(a,b)
@timer
def func2(a,b,c):
print(a,b,c)
return True
func1('aaa','bbb')
print(func2('aaa','bbb','ccc'))

python's eleventh day for me的更多相关文章

  1. Python Celery队列

    Celery队列简介: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 使用 ...

  2. [Spark][Python]spark 从 avro 文件获取 Dataframe 的例子

    [Spark][Python]spark 从 avro 文件获取 Dataframe 的例子 从如下地址获取文件: https://github.com/databricks/spark-avro/r ...

  3. python之celery队列模块

    一.celery队列简介 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 1 ...

  4. 【Python之路】特别篇--Celery

    Celery介绍和基本使用 Celery 是一个分布式异步消息队列,通过它可以轻松的实现任务的异步处理 举几个实例场景中可用的例子: 你想对100台机器执行一条批量命令,可能会花很长时间 ,但你不想让 ...

  5. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  6. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  7. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  8. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  9. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

随机推荐

  1. Android调用系统相机拍照保存照片很小解决方案

    保存图片小的一般操作步骤: 1. 调用系统相机 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityFo ...

  2. Educational Codeforces Round 23F

    http://codeforces.com/contest/817/problem/F 无限长的数组,刚开始每一位是0,三种操作,1,把(l,r)之间不是1的变成1,2,把(l,r)之间不是0的变成0 ...

  3. 51nod 1154 dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1154 1154 回文串划分 基准时间限制:1 秒 空间限制:131072 ...

  4. language model ——tensorflow 之RNN

    代码结构 tf的代码看多了之后就知道其实官方代码的这个结构并不好: graph的构建和训练部分放在了一个文件中,至少也应该分开成model.py和train.py两个文件,model.py中只有一个P ...

  5. Angular路由的定义和使用

    一.什么是routing(路由) Almost all non-trivial, non-demo Single Page App (SPA) require multiple pages. A se ...

  6. 【辅助工具】Python实现微信跳一跳

    最近迷上了微信跳一跳小游戏,正好也看到知乎上有大神分享了技术贴,我也参考了好多资料,原理就是通过abd命令截取图片,python计算两个点距离,然后转化按压时间,让电脑来完成游戏.我花了很长时间才把程 ...

  7. javascript: what can javascript do?

    1.Javascript can change html content <!DOCTYPE html> <html> <body> <h2>What ...

  8. 剑指offer--26.顺时针打印矩阵

    1,2,3,45,6,7,88,10,11,1213,14,15,16 每次输出第一行,然后删除第一行,逆时针旋转剩下的矩阵. ------------------------------------ ...

  9. Freemarker Failed at: ${itm.creatTimeString?string("yyyy-MM... [in template

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} p.p2 {margin: ...

  10. sysbench安装for oracle

    RHEL7.2+ 1.依赖包安装 * autoconf * automake * cdbs * debhelper (>= 9) * docbook-xml * docbook-xsl * li ...