There were other stackoverflow questions similar to this, but is not specific to my problem where I need to override a function.
I want to convert the following Java code into Python:
import Someclass;
Someclass obj = new Someclass("stringarg") {
@Override
void do(x) {
// Do extra stuff on function
super.do(x);
}
}
Do I need to explicitly create a dummy class for this? Can this be converted into Python just in a single statement like this Java code?
Used: Python 2.7.6, Java 1.8
P.S. This is to be done in Jython, where Someclass
is an imported Java library.
Any help would be appreciated. Thanks.
UDPATE:
- For those who have same problems as mine, but the import class is an abstract class:
Thanks to DNA's link, I managed to make it work by also just adding a super statement in his method like the following:
def patch(self, x):
# do something
print 'goodbye'
super(Someclass, self).do(x)
obj = type('DummySomeclass', (Someclass, object), {"do": patch})("hello")
For those whose import class is not abstract, either DNA and algrebe's code works already but also just adding the missing super:
... super(Someclass, self).do(x)