Strange Inheritance During The Override Of A Method
I'm developing different objects that are inherited from each other.
At one point I noticed that the code of a super method is still executed by the inherited method.
As it can be seen from the sub-method listed below, I commented super()
to prevent the superclass code from being executed.
This is the super-class:
class superClass(superClassOfSuperClass):
def __init__(self, data):
#[...]
#unique call of the superMethod
self.component.addActionListener(self.superMethod)
#[...]
def superMethod(self, param):
print "I'm executed, but I don't have to be executed!"
This is the sub-method:
class subClass(superClass):
def __init__(self, data, newData):
superClass.__init__(self, data)
self.newData = newData
#[...]
def superMethod(self, param):
#super(superClass, self).superMethod(None)
print 'only I have to be printed!!!'
The code works.
But I don't want to see the superMethod
print.
Is there a way to prevent superMethod from running and make the subMethod execute only?
Because up to now I can see both print outputs.
I hope it's my shortcoming on OOP.
PS: I'm working with Jython 2.7 as you can see from tags.
Answer
There must be another explanation, for instance if there are more classes involved and there is another class which inherits from superClass
, but does not override superMethod
.
The following code (I'm using python 3) shows that everything should work just fine:
class Super:
def __init__(self, listener):
listener.append(self.superMethod)
def superMethod(self, param):
print("I'm executed, but I don't have to be executed!")
class Sub(Super):
def superMethod(self, param):
print('Only me!')
methods = []
s = Sub(methods)
for method in methods:
method(None)
Output:
Only me!
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module