【发布时间】:2011-09-29 17:22:11
【问题描述】:
在阅读一些 python 模块时,我遇到了这个装饰器类:
# this decorator lets me use methods as both static and instance methods
class omnimethod(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
return functools.partial(self.func, instance)
我对装饰器的了解是可以扩展 功能(例如,对于一个函数)。谁能给我解释一下为什么上面的课程很有用以及它是如何工作的?
在代码中是这样使用的:
@omnimethod:
def some_function(...):
pass
另一个问题:
I encountered this piece of code in the same file:
@property
def some_other_function(...):
pass
@property 未在文件中的任何位置定义。这是一些标准的装饰器吗?如果是,它有什么作用? Google 无法帮助我处理此案。
顺便说一下,这里是我找到代码的来源:http://code.xster.net/pygeocoder/src/c9460febdbd1/pygeocoder.py
【问题讨论】:
-
如果你找不到
property是什么,你的Google-fu 需要工作:搜索“python 属性”,你很快就会找到docs.python.org/library/functions.html#property 之类的东西。从那里到docs.python.org/glossary.html#term-decorator
标签: python properties decorator