Я только что создал простой класс python, чтобы узнать больше о методах magi c, и я использовал REPL для создания экземпляра объекта. Но когда я набираю help(<instance>)
, он не показывает метод __init__()
в начале. Как получить метод init, который будет показан в начале справки. Это важно, потому что человек должен увидеть метод init, чтобы узнать, как его создать.
class vector:
def __init__(self,x,y):
self.x = x
self.y = y
def __rmul__(self,const):
if type(const) == int:
return vector(self.x*const,self.y*const)
else:
raise TypeError(f"an int type is expected but {type(const)} is provided")
def __mul__(self,const):
return vector(self.x*const,self.y*const)
def __add__(self,other):
if type(self) == type(other):
return vector(self.x+other.x, self.y+other.y)
else:
raise TypeError(f"type of other should be {type(other)}")
def __radd__(self,other):
if type(self) == type(other):
return vector(self.x+other.x, self.y+other.y)
else:
raise TypeError(f"type of other should be {type(other)}")
def __eq__(self,other):
if type(self) == type(other) and self.x == other.x and self.y == other.y:
return True
else:
return False
def __len__(self):
return (self.x**2 + self.y**2)
def __repr__(self):
return f"vector({self.x},{self.y})"