Let's suppose that Parent
class is:
class Parent:
def __init__(self, a):
self.a = a
def do_something(self):
a = self.a
b = self.a
print('The a is ', a)
print('The b is ', b)
I want to create a Child
class that will redefine only b
Something like:
class Child(Parent):
def do_something(self, custom_param):
@some_exotic_decorator_or_something_else(b=custom_param)
super().do_something()
And get output like:
obj = Child('the_a')
obj.do_something('the_b')
The a is the_a
The b is the_b
Of course original method has many lines and complicated logic to override whole method. Also that is one of common python libraries so I want to minimise intrusion inside method.
Here is any common way in python to do that?
To be more clear, the method what I want to override is _write_end_record
I want to redefine only centDirOffset
. All other logic is working good. I may copy and paste whole method inside my code and change only one line, but it don't looking like a smart idea
A clean and easy way would be to make your parent class more general by adding a parameter to your method and assign a default value to it.
class Parent:
def __init__(self, a):
self.a = a
def do_something(self, b=None):
if b is None:
b = self.a
a = self.a
print('The a is ', a)
print('The b is ', b)
By doing this, you do not even need a child class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.