I tried to find the answer here, but could not.
@obj.func
I donβt understand why the third form is SyntaxError, it seems to me that this does not violate the python syntax, and it is clear to me what the user wants to do (see the example below).
I looked at pep 0318 decorator implementations, but could not find the answers.
The following is an example of use:
class ItemFunc(object): def __init__(self, fcall=None, **kwargs): self.defaults = kwargs self.fcall = None def __call__(self, *args, **kwargs): kwargs = dict(self.defaults, **kwargs)
How can you use ItemFunc as a decorator:
@ItemFunc def plot(**kwargs): pass redcross = plot.copy(color="red", marker="+") @redcross.caller def plot_data1(**kwargs): pass bluecross = redcross.copy(color="blue") @bluecross.caller def plot_data2(**kwargs): pass
But why is this following "short syntax" forbidden:
@redcross.copy(color="blue").caller def plot_data2(**kwargs): pass
But I can do:
@redcross.copy_and_decorate(color="blue") def plot_data2(**kwargs): pass
The first form looks better, at least I better understand the intentions.
source share