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 ...
随机推荐
- Entity Framework Code-First(2):What is Code-First?
What is Code-First?: Entity Framework introduced Code-First approach from Entity Framework 4.1. Code ...
- Spring入门第二十七课
声明式事务 直接上代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dri ...
- adb devices unauthorized解决方法
有时候使用adb连接手机时,即使打开了usb调试,手机添加了信任,仍然出现unauthorized的提示 解决办法如下: 先上两张stack overflow上面的图片: 很多人可能看不懂.翻一下大概 ...
- HDP3.1 中 YRAN 和 MR2 的内存大小配置的计算方式
Container 是 YARN 中基本的处理单元,它是对内存.CPU等计算的封装.总的来说,每个core每块硬盘 分配2个 container,能获得较好的集群利用率. 1. 确定可用内存大小. 对 ...
- centos 基础设置
centos 6 关闭防火墙 查看防火墙是否开启 service iptables status 停止防火墙 service iptables stop 禁止开机自启动防火墙 chkconfig ip ...
- dorado 常用
如果要设置模糊查询, 一般要在QueryCommand中这样写: var name = dsQuery.getValue("NAME"); var parameters = com ...
- JIRA reference
Workflow https://confluence.atlassian.com/adminjiracloud/configuring-workflow-schemes-776636598.html ...
- luoguP4931 情侣?给我烧了!(加强版)
luogu 普通版题解:https://www.cnblogs.com/lcxer/p/10876856.html 在普通版里,我们考虑对于\(n\)对情侣,恰好\(k\)对是和谐的方案数是 \[ a ...
- $each 遍历json字符串
$.each遍历json对象 查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1",&qu ...
- Vue中的指令(听博主说总结的很好)
指令[重点] 作用:简化Dom操作 参考:https://cn.vuejs.org/v2/api/#%E6%8C%87%E4%BB%A4 特点: 1.都是以v-开头 2.除了插值表达式,其它都写在标签 ...