问题
Im new to Python, and im building a wrapper for an api. I would want to let the user decide if he/she wants to use a decorator on methods I expose from my module.
For example:
# create a new instance
api = MyApi()
# return a simple json response with all the data related to a timetable
print api.time_table
Now the user has all the data, and can do whatever he/she wants. I would want to add some kind of 'convenience' methods, for example; to let the user get a small part of the json instead of the whole big json response.
My idea was to use pythons decorators for this. Optionally my goal whould be something like this:
# 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
Naturally, thats not how decorators work, but is there a way to optionally use a decorator on a existing class method, or does the decorator always have to be wrapping the original method?
So what is the most Pythonic way here? I really would like to use decorator, but if thats a bad idea here, im fine with just creating more 'convenience' methods inside my Api
class.
回答1:
Your options are to add more methods to your API or to provide the user with utility functions instead:
from yourmodule import MyAPI
api = MyAPI
filtered_timetable = api.filter_on(something)
or
from yourmodule import MyAPI, filter_timetable
api = MyAPI
filtered_timetable = filter_timetable(api.time_table, something)
Remember that decorators are just callables; the syntax:
@foo
def bar():
pass
is just syntactic sugar for:
def bar():
pass
bar = foo(bar)
foo()
is called, and the return value replaces the decorated object. Usually you use functions for decorators, but nothing says you have to use those functions only as decorators.
filter_timetable
could be such a decorator; if you have a usecase for using it as both.
来源:https://stackoverflow.com/questions/22731471/optionally-use-decorators-on-class-methods