I would like to use a @decorator
to allow me to be able to access ' - '.join(args)
as args
, as depicted below. Is this possible, using a metaclass perhaps?
def a(*args):
print(args)
a(1, 2, 3)
# (1, 2, 3)
@magic
def b(*args):
print(args)
b(1, 2, 3)
# 1 - 2 - 3
You can get close:
def magic(func):
def wrapper(*args):
return func(' - '.join(map(str, args)))
return wrapper
but this prints out ('1 - 2 - 3',)
because the body of b
sees args
as a tuple due to the *args
, and I doubt a decorator can get around that. What do you expect to happen if the body is something like print(args[1])
?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments