First I’ve learned what Python decorators are and how they work. And I’d like it to do something like this:
def age_over_18(go_enjoy_yourself):
def go_home_and_rethink_your_life():
return 'So you should go home and rethink your life.'
return go_enjoy_yourself if age_stored_somewhere > 18 else go_home_and_rethink_your_life
@age_over_18
def some_porn_things():
return '-Beep-'
But I found out that decorators are executed when the function is first read by Python, that means this function will actually do nothing.
I know I can write something like:
def some_porn_things():
if age_stored_somewhere > 18:
...
else:
...
But I just think decorators are graceful and easy to understand, so the question is:
Can I delay a decorator to happen until I call the function?
The trick is just to ensure that the check happens in your inner function, not the outer one. In your case: