J'essaye de tester l'utilité des décorateurs en Python. Lorsque j'écrire les codes suivants, il y a une erreur:Python decorator: TypeError: la fonction prend 1 argument positionnel mais 2 ont été donnés
TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given
Je définis première fonction log_calls(fn)
comme
def log_calls(fn):
''' Wraps fn in a function named "inner" that writes
the arguments and return value to logfile.log '''
def inner(*args, **kwargs):
# Call the function with the received arguments and
# keyword arguments, storing the return value
out = fn(args, kwargs)
# Write a line with the function name, its
# arguments, and its return value to the log file
with open('logfile.log', 'a') as logfile:
logfile.write(
'%s called with args %s and kwargs %s, returning %s\n' %
(fn.__name__, args, kwargs, out))
# Return the return value
return out
return inner
Après cela, j'utilise log_calls pour décorer une autre fonction en tant que:
@log_calls
def fizz_buzz_or_number(i):
''' Return "fizz" if i is divisible by 3, "buzz" if by
5, and "fizzbuzz" if both; otherwise, return i. '''
if i % 15 == 0:
return 'fizzbuzz'
elif i % 3 == 0:
return 'fizz'
elif i % 5 == 0:
return 'buzz'
else:
return i
Lorsque j'exécute le code suivant
for i in range(1, 31):
print(fizz_buzz_or_number(i))
L'erreur TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given
vient.
Je ne sais pas quel est le problème avec ce décorateur, et comment résoudre ce problème.
Je vois ce que vous voulez dire. J'ai réessayé selon ta suggestion, ça marche maintenant. Merci beaucoup. – Luhuimiaomiao