3
class test():
    c = 0
    def __init__ (self, a, b):
        self.a = a
        self.b = b
        test.c += self.a * self.b
    @classmethod
    def __repr__ (cls):
        return "{}".format(cls.c)

p_1 = test(10, 20)
p_2 = test(5, 6)
print (test) # должно быть 230

1 ответ 1

5
class A(type):
    def __repr__(self):
        return "{}".format(self.c)

class test(metaclass = A):
    c = 0
    def __init__ (self, a, b):
        self.a = a
        self.b = b
        test.c += self.a * self.b

p_1 = test(10, 20)
p_2 = test(5, 6)
print(test)
2
  • Есть ли варианты без метаклассов?
    – Molotoff
    22 фев 2021 в 11:23
  • @Molotoff возможно есть, но я не знаю
    – Danis
    22 фев 2021 в 11:26

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.