So I'm trying to use a magic method from one class in another class that inherits that class. basically what im trying to do below...
class Class1:
def __magicmethod__(self, arg1, arg2):
#blah blah
class Class2(Class1):
def func1(self, x, y):
#what do type here to use the magic method???
thank you, I'm really confused about his
CodePudding user response:
Just to transform both comments to a self-contained answer: If __magicmethod__
is one of the well-known ones with associated operator (like __eq__
is, simply use the operator to access that. If not, simply "call it directly":
class Class1:
def __eq__(self, other):
print(f"== from {self} with {other} here!")
def __magicmethod__(self, arg1, arg2):
print(f"{self}({arg1}, {arg2})")
class Class2(Class1):
def eq(self, other):
self == other
def func1(self, x, y):
self.__magicmethod__(x, y)
And then use like:
>> c2 = Class2()
>>> c2.func1("x", "y")
<__main__.Class2 object at 0x0000021B8694A1F0>(x, y)
>>> c2 == "foo"
== from <__main__.Class2 object at 0x0000021B8694A1F0> with foo here!
>>> c2.eq("foo") # same as before, but through your own method
Nothing special really.