python decorator 用法
https://www.zhihu.com/question/31265857
http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html
方法装饰器
def decorator(Func):
def new_Func(a, b):
print 'input:', a, b
return Func(a, b)
return new_Func
@decorator
def square_sum(a, b):
return a**2 + b**2
@decorator
def square_diff(a, b):
return a**2 - b**2
print square_sum(3, 4)
print square_diff(3, 4)
# output
input: 3 4
25
input: 3 4
-7
类装饰器
def decorator(Class):
class new_Class:
def __init__(self, age):
self.total_display = 0
self.wrapped = Class(age)
def display(self):
self.total_display += 1
print("total display", self.total_display)
self.wrapped.display()
return new_Class
@decorator
class Bird(object):
def __init__(self, age):
self.age = age
def display(self):
print("My age is", self.age)
eagleLord = Bird(5)
for i in range(3):
eagleLord.display()
# output
('total display', 1)
('My age is', 5)
('total display', 2)
('My age is', 5)
('total display', 3)
('My age is', 5)
python decorator 用法的更多相关文章
- Python Decorator 和函数式编程
看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- python argparse用法总结
转:python argparse用法总结 1. argparse介绍 argparse是python的一个命令行解析包,非常适合用来编写可读性非常好的程序. 2. 基本用法 prog.py是我在li ...
- Anaconda下载及安装及查看安装的Python库用法
Anaconda下载及安装及查看安装的Python库用法 Anaconda 是一个用于科学计算的 Python 发行版,提供了包管理与环境管理的功能.Anaconda 利用 conda 来进行 pac ...
- python enumerate用法总结【转】
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...
- Python高级用法
Python高级用法 三元表达式 x = 10 y = 20 print(x if x > y else y) x = 100 y = 20 print(x if x > y else y ...
- Python import用法以及与from...import的区别
Python import用法以及与from...import的区别 在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的 ...
- 预备知识-python核心用法常用数据分析库(上)
1.预备知识-python核心用法常用数据分析库(上) 目录 1.预备知识-python核心用法常用数据分析库(上) 概述 实验环境 任务一:环境安装与配置 [实验目标] [实验步骤] 任务二:Pan ...
- python decorator 基础
一般来说,装饰器是一个函数,接受一个函数(或者类)作为参数,返回值也是也是一个函数(或者类).首先来看一个简单的例子: # -*- coding: utf-8 -*- def log_cost_tim ...
随机推荐
- Swoole 整合成一个小框架
目录 概述 效果 代码 小结 概述 这是关于 Swoole 学习的第六篇文章:Swoole 整合成一个小框架. 第五篇:Swoole 多协议 多端口 的应用 第四篇:Swoole HTTP 的应用 第 ...
- kolla base目录下Dockerfile.j2分析
这几天在研究kolla制作镜像的流程,记录下对kolla项目中base目录Dockerfile.j2阅读过程.本质上base目录下的Dockerfile.j2文件,对是yum 源的一些配置,从而使制作 ...
- php5.6安装window7安装memcache.dll库所遇到的误区
问题: window7 64位,下载的库 memcache.dll 为64位的,且对应php的版本.但是重启后phpstudy查看phpinfo依然没有memcache: 根源: 发现是下载的 mem ...
- RHEL 7.3 无图形界面安装oracle 11gr2
行方服务器无X界面,只能使用纯命令安装方式.提供RHEL全量系统镜像. 在网上搜了搜,参照CSDN作者“西伯利亚疯狂的蚯蚓”的安装过程成功安装oracle11gR2 原文:https://blog.c ...
- Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)
A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...
- rem 回家测试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ext3.1的一些使用讨论
这里简单记录一下曾经的10个月使用Ext的工作模式. 前公司用的是 Ext 3.1,在2018/2019的今天,可以说是比较久远的技术了.处于大前端发展时代的我们,对其的诟病应该不少. 不过其中面向对 ...
- JS事件之鼠标悬浮窗(鼠标悬浮窗抖动问题的解决)
鼠标进入显示悬浮窗,思路有简单有困难. 首先要注意的是我们要给悬浮窗设置position为absolute,不然我们改了 style.left style.top发现没有变化很尴尬.其余的内容看起来就 ...
- libaudit_plugin.so安装
#上传audit到mysql的plugin目录vim /etc/my.cnfplugin-load=AUDIT=libaudit_plugin.soaudit_json_file=1audit_jso ...
- urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
解决办法: import ssl # 在请求之前加上 ssl._create_default_https_context = ssl._create_unverified_context