python装饰器使用方法实例
Jun 16, 2016 am 08:46 AM什么是python的装饰器?
网络上的定义:
装饰器就是一函数,用来包装函数的函数,用来修饰原函数,将其重新赋值给原来的标识符,并永久的丧失原函数的引用。
最能说明装饰器的例子如下:
#-*- coding: UTF-8 -*-
import time
def foo():
print 'in foo()'
# 定义一个计时器,传入一个,并返回另一个附加了计时功能的方法
def timeit(func):
# 定义一个内嵌的包装函数,给传入的函数加上计时功能的包装
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
# 将包装后的函数返回
return wrapper
foo = timeit(foo)
foo()
python中提供了一个@符号的语法糖,用来简化上面的代码,他们的作用一样
import time
def timeit(func):
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper
@timeit
def foo():
print 'in foo()'
foo()
这2段的代码是一样的,等价的。
内置的3个装饰器,他们分别是staticmethod,classmethod,property,他们的作用是分别把类中定义的方法变成静态方法,类方法和属性,如下:
class Rabbit(object):
def __init__(self, name):
self._name = name
@staticmethod
def newRabbit(name):
return Rabbit(name)
@classmethod
def newRabbit2(cls):
return Rabbit('')
@property
def name(self):
return self._name
装饰器的嵌套:
就一个规律:嵌套的顺序和代码的顺序是相反的。
也是来看一个例子:
#!/usr/bin/python
# -*- coding: utf-8 -*-
def makebold(fn):
def wrapped():
return "" + fn() + ""
return wrapped
def makeitalic(fn):
def wrapped():
return "" + fn() + ""
return wrapped
@makebold
@makeitalic
def hello():
return "hello world"
print hello()
返回的结果是:
hello world
为什么是这个结果呢?
1.首先hello函数经过makeitalic 函数的装饰,变成了这个结果hello world
2.然后再经过makebold函数的装饰,变成了hello world,这个理解起来很简单。

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Quels sont les avantages et les inconvénients des modèles ?

Google AI annonce Gemini 1.5 Pro et Gemma 2 pour les développeurs

Pour seulement 250$, le directeur technique de Hugging Face vous apprend étape par étape comment peaufiner Llama 3

Partagez plusieurs frameworks de projets open source .NET liés à l'IA et au LLM

Un guide complet sur le débogage et l'analyse des fonctions Golang

Comment enregistrer la fonction d'évaluation
