0
私はPythonでデコレータのユーティリティをテストしようとしています。Pythonデコレータ:TypeError:関数は1つの位置引数をとりますが、2が与えられました
@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
:私が最初に私のように別の機能を飾るためにlog_callsを使用し、その後
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
として機能log_calls(fn)
を定義
TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given
:私は以下のコードを書くとき、誤りがあります
次のコードを実行すると、
for i in range(1, 31):
print(fizz_buzz_or_number(i))
エラーTypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given
が来ます。
私はこのデコレータに何が問題なのか、これを修正する方法はわかりません。
私はあなたが何を意味するかを見ます。私はあなたの提案に従って、もう一度試しました。どうもありがとう。 – Luhuimiaomiao