I am familiarising with python decorators, and wondering about a general cases.
Let's say I have a function to generate a person:
def GeneratePerson(name, surname):
return {"name": name, "surname": surname}
Now, let's say I wanted a decorator to add an age:
def WithAge(fn):
def AddAge(*args):
person = fn(args[0], args[1])
person[age] = args[2]
return person
return AddAge
And I would then call
@AddAge
GeneratePerson("John", "Smith", 42)
However, I found this a bit counterintuitive. If I look at the signature of "GeneratePerson", I see that it only takes a name and a surname parameter. The age (42) is a property required by the decorator. I feel a syntax (Java-style) as:
@AddAge(42)
GeneratePerson("John", "Smith")
Might be preferable.
I appreciate that I could address that using kwargs, calling a stack as:
@WithAge
@AddGender
GeneratePerson(name="John", surname="Smith", age="42", gender="M")
But is there a way to pass a parameter to a decorator function directly (eg: @Age(42)
rather than having to pass it indirectly via the decorated function?)