0

I have following code:

    class SomeClass:
        def __init__(self) -> None:
            pass
    
        def some_class_function(self, par):
            print(par)
    
    class SomeOtherClass:
        def __init__(self) -> None:
            pass
    
        def some_other_class_function(self, par):
            print(par+1)
    
    if __name__ == "__main__":
        sc = SomeClass()
        sc.some_class_function = SomeOtherClass.some_other_class_function
        sc.some_class_function(1)

When I execute the code I get

TypeError: some_other_class_function() missing 1 required positional argument: 'par'

How can I override the method of the first class with the method of the second class properly?

  • I suspect that this might work if you replace the second line of the main script with `sc.some_class_function = SomeOtherClass().some_other_class_function`, adding in those parentheses. – Ben Grossmann Oct 12 '22 at 13:20
  • Do you want to override it for one instance only, or for the class in general? – deceze Oct 12 '22 at 13:20
  • @Ben That's going to bind `self` to `SomeOtherClass()` though, not to `sc`. – deceze Oct 12 '22 at 13:21
  • @deceze Based on the accepted answer, that seems to be the desired behavior – Ben Grossmann Oct 12 '22 at 13:33
  • 1
    @Ben Since neither method interacts with `self`, OP may just not have discovered that side effect yet… – deceze Oct 12 '22 at 13:34
  • @Ben Ah I think I just discovered it.... Is it possible that self is still sc and not sco? – michaelgr22 Oct 12 '22 at 13:42
  • @michaelgr22 You can redefine `sc.some_class_function` to be another function that uses `sc` as self, but there's no good way to use the existing `SomeOtherClass` to do that. – Ben Grossmann Oct 12 '22 at 13:50
  • @michaelgr22 If `SomeClass` is a subclass of `SomeOtherClass`, then you could access `some_other_class_function` while using `sc` as "self". – Ben Grossmann Oct 12 '22 at 13:53
  • @Ben inheritance is unfortunately not an option for me. Do you have an example for the first approach, when some_other_class_function is not a function of SomeOtherClass? – michaelgr22 Oct 12 '22 at 14:36
  • @michaelgr22 See [this post](https://stackoverflow.com/a/2982/2476977) – Ben Grossmann Oct 12 '22 at 17:30

2 Answers2

1

You need to initialize the class to call its method.

sc = SomeClass()
sco = SomeOtherClass() # initialize the second call to call it's method
sc.some_class_function = sco.some_other_class_function
sc.some_class_function(1)
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
1

As you have noted in the comments, you are interested in adding method that will use sc as the "self" instance.

To that end, see this post. To summarize, you can either add a function to the class definition (affecting future instances of the same class), or bind the function to the particular instance.

As an example, consider the following class and function.

class Test():
    def __init__(self):
        self.phrase = "hello world"
    def func(self):
        print("this is the old method")

def test_func(self):
    print(self.phrase)

For the first approach, we could do the following

test = Test()
Test.func = test_func
test.func()

Note that future instances of Test will have this function as an attribute. For example, running Test().func() will still result in the same output, even though the method is being used on a new class instance.

For the second, we could do the following.

import types

test = Test()
test.func = types.MethodType(test_func, test)
test.func()

In this case, running the line Test().func() will result in the output "this is the old method" because func has not been overwritten for new instances.

Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16