Я пытаюсь создать простую функцию, которая возвращает вывод множителя, как показано ниже.
Вопрос, который я хочу задать, заключается в том, что при печати (times3) он не дает значения, а вместо этого показывает «.multiplier at 0x00000236884B3558>», который я не уверен, почему. Есть ли способ напечатать номер вместо?
Другой вопрос заключается в том, что def multiplier (n) возвращает n * n, не должно ли оно дать 5 * 5 = 25? Поэтому, если бы я должен был напечатать (times5 (times3 (2))), он должен дать мне 25 * 81?
def make_multiplier_of(n):
def multiplier(n):
return n*n
return multiplier
# Multiplier of 3
times3 = make_multiplier_of(3)
print(times3)
# Multiplier of 5
times5 = make_multiplier_of(5)
# Output: 81
print(times3(9))
# Output: 9
print(times5(3)) #shouldn't it be 25?
# Output: 16 #shouldn't it be 81 x 25?
print(times5(times3(2)))
обновленный код:
def make_multiplier_of(n):
def multiplier(m):
return m*n
return multiplier
# Multiplier of 3
times3 = make_multiplier_of(3)
#trying to print out times3 value here but it doesnt work
times3.__closure__[0].cell_contents
[cell.cell_contents for cell in times3.__closure__]
# Multiplier of 5
times5 = make_multiplier_of(5)
# Output: 27
print(times3(9))
# Output: 15
print(times5(3)) #shouldn't it be 25?
# Output: 30
print(times5(times3(2)))