Most of the answers inherit the base class to define the abstract methods. But this is not always useful. What if you want to define an abstract method at runtime?
For example in java we can do this
class UserClass { ...
BaseClass f = new BaseClass() {
public void method() {
system.out.println( "this is a test" )
}
};
}
So what to do if we need to implement that, so in that case
class BaseClass:
def __init__(self, func ):
self.function = func
def abstract_function(self ):
if not self.function:
raise NotImplementedError("function not implemented")
else:
return self.function()
def run(self ):
self.abstract_function()
def func():
print('this is a test')
bc = BaseClass( func )
bc.run()
should work