I'd like to call a parent's call method from inherited class
Code looks like this
#!/usr/bin/env python
class Parent(object):
def __call__(self, name):
print "hello world, ", name
class Person(Parent):
def __call__(self, someinfo):
super(Parent, self).__call__(someinfo)
p = Person()
p("info")
And I get,
File "./test.py", line 12, in __call__
super(Parent, self).__call__(someinfo)
AttributeError: 'super' object has no attribute '__call__'
And I can't figure out why, can somebody please help me with this?
Calling Parent class Method Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.
The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x. __call__(arg1, arg2, ...) .
You can't. Inside class Child you can call super. m1(), but what you want isn't possible. here object is of type child1,and refernce is of type parent1.
__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .
The super
function takes the derived class as its first parameter, not the base class.
super(Person, self).__call__(someinfo)
If you need to use the base class, you can do it directly (but beware that this will break multiple inheritance, so you shouldn't do it unless you're sure that's what you want):
Parent.__call__(self, someinfo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With