Ошибка: TypeError: unsupported format string passed to Negate.__format__
обучаюсь у Лутса, пошарил в инете ничего не нашел по данному вопросу, как решить данную проблему?
def square(arg):
return arg ** 2 # Простые функции def или lambda
class Sum:
def __init__(self, val): # Вызываемые экземляры
self.val = val
def __call__(self, arg):
return self.val * arg
class Product:
def __init__(self, val): # Свзвнные методы
self.val = val
def method(self, arg):
return self.val * arg
sobject = Sum(2)
pobject = Product(3)
actions = [square, sobject, pobject.method] # Функцияб экземляр, метод
for act in actions: # Все три вызываются одинаково
print(act(5)) # Вызов любого вызываемого объекта с одним аргументом
print(actions[-1](5)) # Идексы, генераторы, отображения
# Result - 15
print([act(5) for act in actions])
# Result - [25, 10, 15]
print(list(map(lambda act: act(5), actions)))
# Result - [25, 10, 15]
class Negate:
def __init__(self, val): # Классы - тоже вызываемые объекты
self.val = -val # Но вызываются для создания объектов
def __repr__(self): # Реализует вывод экземляра
return str(self.val)
actions1 = [square, sobject, pobject.method, Negate] # Вызвать класс тоже
for act1 in actions1:
print(act1(5))
print([act1(5) for act1 in actions1]) # Вызовет __repr__, а не __str__!
# Result - [25, 10, 15, -5]
table = {act1(5): act for act1 in actions1} # Генератор словарей
for (key, value) in table.items():
print('{0:2} => {1}'.format(key, value)) # Метод str.format
Ошибка:
25 => <bound method Product.method of <__main__.Product object at 0x000001FB7613F650>>
10 => <bound method Product.method of <__main__.Product object at 0x000001FB7613F650>>
15 => <bound method Product.method of <__main__.Product object at 0x000001FB7613F650>>
Traceback (most recent call last):
File "G:\Python\Обучение\Глава 30. Шаблоны проектирования с классами\sum.py", line 49, in <module>
print('{0:2} => {1}'.format(key, value)) # Метод str.format
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported format string passed to Negate.__format__
Process finished with exit code 1