class A:
def open_spider(self, spider):
#do some hacking
class B(A):
def open_spider(self, spider):
super(B, self).open_spider(spider)
#something else
Now I want C to call A's method but not B's, which can be done at least in two ways:
class C(B):
def open_spider(self, spider):
A.open_spider(self, spider)
#do things
class C(B):
def open_spider(self, spider):
super(B, self).open_spider(spider)
#do things
Overriding a method in the same class is not allowed. So, you need to do that in the child class by implementing the Inheritance concept. If you want to override the Parent Class method, create a function in the Child with the same name and number of parameters. This is called function overriding in Python.
The way to "avoid" inheritance here would be to rename _private_var and make it a class-private name. i.e. __private_var . If you do this, running your code will cause an AttributeError: 'Child' object has no attribute '_Parent__private_var' (note the _Parent prefix automatically added).
The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.
If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.
You can do it using the second way. But I must say that part where child skips parent method and call grandparent instead is wrong and you should take a look on your design once again and think about it.
Use the second method; this is one reason why super
takes a class as its first argument.
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