1
(Python 2.7)由於裝飾器不能與他們正在裝飾的函數共享變量,我怎樣才能使/將object_list
傳遞給裝飾函數?我有一些功能將使用raw_turn_over_mailer()
裝飾器,我想保留object_list
到可能的地方裝飾功能。函數和它的裝飾器之間的Python-共享變量
def raw_turn_over_mailer(function):
@wraps(function)
def wrapper(requests):
original_function = function(requests)
if object_list:
....
return original_function
return wrapper
@raw_turn_over_mailer
def one(requests):
object_list = [x for x in requests
if x.account_type.name == 'AccountType1']
@raw_turn_over_mailer
def two(requests):
object_list = [x for x in requests
if x.account_type.name == 'AccountType2']
@periodic_task(run_every=crontab(hour="*", minute="*", day_of_week="*"))
def turn_over_mailer():
HOURS = 1000
requests = Request.objects.filter(completed=False, date_due__gte=hours_ahead(0), date_due__lte=hours_ahead(HOURS))
if requests:
one(requests)
two(requests)
請提供SSCCE([Short,Self Contained,Correct,Example](http://www.sscce.org/))代碼。 – martineau