Элементарный вопрос по Python по классам

class a():
    def __init__(self,x):
        self.x = x
    def xpr(n):
        print(self.x*n)

a=a('rt')
a.xpr(10)
rtrtrtrtrtrtrtrtrtrt

class b(a):
    def reconstr(self):
        y = self.x*2
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    class b(a):
TypeError: a.__init__() takes 2 positional arguments but 4 were given

Good day! Why there is an error ? It must be a simple example of inheritance(наследование) Is there any difference in versions of language or what ? (code text - copy-paste text from shell)


Ответы (1 шт):

Автор решения: Danis

Проблема в строке:

a = a("rt")

С этого момента в переменой a лежит не класс, а экземпляр этого класса. В этом вся проблема. Либо измените название переменной, либо название класса.

→ Ссылка