Reputation: 3551
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
Upvotes: 3
Views: 83
Reputation: 51
So part of the problem is that you're using integers where you need strings, so convert them into strings and then use the join function.
def magic(func):
def wrapper(*args):
return ' - '.join(map(str, args))
return wrapper
@magic
def a(*args):
return 'Arguments were {}.'.format(args)
print(a(1, 2, 3))
Upvotes: 0
Reputation: 36033
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])
?
Upvotes: 2