-1

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

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • 1
    Not really… functions are opaque objects that you can't easily reach into the modify individual variables or lines or anything else. — Yes, it's probably possible with enough bending over backwards, but this *really* shouldn't be your first goto solution to whatever problem you're trying to solve. – deceze Dec 16 '21 at 13:04
  • Am I understanding you correctly that the parent class is not yours, but something in an existing library? – CrazyChucky Dec 16 '21 at 13:17
  • @CrazyChucky Yep. added additional information after `To be more clear` – rzlvmp Dec 16 '21 at 13:24

1 Answers1

1

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.

Durtal
  • 1,063
  • 3
  • 11
  • Okay. I added exact Class and method that I want to override inside question. Any ideas how to `make it more general`? – rzlvmp Dec 16 '21 at 13:26
  • @rzlvmp [This thread](https://stackoverflow.com/questions/41858147/how-to-modify-imported-source-code-on-the-fly) might lead you where you want, but I do not consider this as a nice way to code. – Durtal Dec 16 '21 at 13:43
  • I solved my problem using another [strategy](https://stackoverflow.com/a/70388239/13946204). But I still looking for way how to partially override existing methods. Link mentioned before don't have answer how to do that – rzlvmp Dec 17 '21 at 04:10
  • @rzlvmp As deceze indicates, this is getting quite involved. My idea was to import the function you want and modify it while importing using regexp. This way you can overwrite the value used for `centDirOffset`. The thread offers some strategies to achieve this. But I don't see why you should do this. You make yourself depended on the actual sourcecode (not only on the behavior) of the imported function and on future changes of it. In my eyes this is a recipe for future disaster. Instead hang on with a one time CTRL-C + CTRL-V and you will be fine for a long time. – Durtal Dec 17 '21 at 15:21