【发布时间】:2014-05-09 00:21:50
【问题描述】:
我是 Python 新手,我正在为 api 构建一个包装器。我想让用户决定他/她是否想对我从模块公开的方法使用装饰器。
例如:
# create a new instance
api = MyApi()
# return a simple json response with all the data related to a timetable
print api.time_table
现在用户拥有了所有的数据,并且可以为所欲为。例如,我想添加一些“方便”的方法;让用户得到一小部分 json 而不是整个大的 json 响应。
我的想法是为此使用 python 装饰器。 (可选)我的目标应该是这样的:
# use the method and get al the data
print api.time_table
# Optionally, get a specific part, just the shows part. (PSEUDO CODE BELOW)
@shows
print api.time_table
当然,装饰器不是这样工作的,但是有没有办法在现有的类方法上选择使用装饰器,或者装饰器总是必须包装原始方法?
那么这里最 Pythonic 的方式是什么?我真的很想使用装饰器,但如果这不是一个好主意,我可以在我的 Api 类中创建更多“方便”方法。
【问题讨论】:
-
您只能在函数和类上使用装饰器,而不能在其他任何东西上使用装饰器(例如
print语句)。你可以扩展你的 API 来给用户更多的选择。 -
感谢您的快速回答,所以没有办法在现有函数上使用装饰器?在这种情况下,我不得不使用更多的类方法。使用 lambdas 可能会想出一些方便的东西。
-
当然你也可以使用装饰器;装饰器只是函数;应用于
bar的@foo与bar = foo(bar)完全相同;当以bar作为参数调用时,将bar替换为foo的返回值。
标签: python oop decorator python-decorators