装饰器

装饰器英文Decorator,自身是一个函数,用来包装其它的函数,实现在不改变原来代码的情况下,增加新的功能,返回一个修改后的函数对象,

装饰器功能:

1、装饰器也是函数

2、在不改变原有代码的情况下,增加新的功能

3、符合开放-封闭原则

在学习装饰器之前,我们复习一下函数的相关概念

理解函数也是变量

当我们执行函数不加括号的时候,输出函数的内存地址:

def foo():
print('hello') print(foo) # 输出
<function foo at 0x0000000002CBCBF8>

当我们加上括号后,输出

def foo():
print('hello') foo() # 输出
hello

高阶函数:

1、把一个函数名当做实参传给另外一个函数

def bar():
print('bar') def t1(func):
func() t1(bar) # 输出
bar

2、返回值中包含函数名

def bar():
print('bar') def t1(func):
return func print(t1(bar)) # 输出
<function bar at 0x000000000303CBF8>

函数嵌套

def foo():
print('foo') def bar():
print('bar')
bar() foo()

前面做了那么多的铺垫,都是为了后边的装饰器,装饰器的组成离不开高阶函数+函数嵌套

无参数装饰器

def logger(func):
def inner():
print('logger start')
res = func()
print('logger stop')
return res return inner @logger
def test1():
print('test1') def test2():
print('test2') test1()
test2() # 输出
logger start
test1
logger stop
test2

带固定参数装饰器

def logger(func):
def inner(arg):
print('logger start')
res = func(arg)
print('logger stop')
return res return inner @logger
def test1():
print('test1') @logger
def test2(name):
print('test2', name) # test1()
test2('chen') # 输出
logger start
test2 chen
logger stop

但是这个时候我的test1函数不能调用了,因为它没有参数,怎么解决,让test1没有参数,test2带参数都可以是用呢?

非固定参数装饰器

def logger(func):
def inner(*args, **kwargs):
print('logger start')
res = func(*args, **kwargs)
print('logger stop')
return res return inner @logger
def test1():
print('test1') @logger
def test2(name):
print('test2', name) test1()
test2('chen')

终极版

def logger(write_type):
# print(write_type)
def outer_wrapper(func):
def inner(*args, **kwargs):
if write_type == 'file':
print('logger start')
res = func(*args, **kwargs)
print('logger stop')
return res
elif write_type == 'db':
print('no support db')
return inner return outer_wrapper @logger(write_type='file')
def test1():
print('test1')
return 'return test1' @logger(write_type='db')
def test2(name):
print('test2', name) a = test1()
print(a)
test2('chen') # 输出
logger start
test1
logger stop
return test1
no support db

生成器

1、生成器只有在调用时候,才会生成相应的数据

2、

json和pickle

json于pickle的区别:

1、json是所有语言通用

2、json只能操作基本数据类型,比如字典、列表、元祖等

3、pickle只能在python内使用

4、pickle可以序列化python内的所有类型

Python入门5的更多相关文章

  1. python入门简介

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  2. python入门学习课程推荐

    最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...

  3. Python运算符,python入门到精通[五]

    运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...

  4. Python基本语法[二],python入门到精通[四]

    在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...

  5. Python基本语法,python入门到精通[二]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...

  6. visual studio 2015 搭建python开发环境,python入门到精通[三]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...

  7. python入门教程链接

    python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...

  8. Python学习【第二篇】Python入门

    Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...

  9. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  10. Python入门版

    一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...

随机推荐

  1. 远程联机linux主机

    远程联机linux主机 推荐使用 ssh  如 ssh user@www.abc.com(ssh使用公钥+私钥非对称加密,数据传输安全,不要使用telnet) 传输文件:sftp 或者 scp 若想使 ...

  2. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  3. Python基于websocket实时通信的实现—GoEasy

    Python websocket实时消息推送 在这里我记录一下之前如何实现服务器端与客户端实时通信: 实现步骤如下: 1.        获取GoEasy appkey. 在goeasy官网上注册一个 ...

  4. Asp.net Web.Config - 配置元素 trace

    一.trace的元素的属性 属性 说明 enabled 是否启用应用程序跟踪.为了使用 Trace.axd 查看器,必须启用跟踪.默认情况下,Trace.axd 查看器被添加到httpHandlers ...

  5. android view : window

    既然是view,为什么要说window,实际上着是一个很有用的东西,在展现view和设计界面上很有用,就比如说悬浮窗 但是这时候又要分清楚一个概念,window到底是什么?在activity中说过了我 ...

  6. libev学习(一)

    一.libev简介 Libev是一个事件循环:你注册感兴趣的特定事件(比如一个文件可以读取时或者发生超时时),它将管理这些事件源,将这些事件反馈给你的程序.为了实现这些,至少要在你的进程(或线程)中执 ...

  7. bs4 python解析html

    使用文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ python的编码问题比较恶心. decode解码encode编码 在文件 ...

  8. div中显示页面

    在css中显示页面,在页面布局中很多时候都要在一个div 里显示某些页面.在这里写下我用到的一种方式. <script type="text/javascript"> ...

  9. java高级规范之一

    一.不允许使用汉语拼音命名 不规范示例: public void zengjiaYongHu{}//拼音方法名称 规范示例: public void addUser(){} 解析:应该使用国际化语音, ...

  10. EMV文档:接收到的ATR不在EMV规定范围,终端需要的操作

    Required terminal behaviour in the event that a terminal receives characters outside the range allow ...