decorator which can add an outside method to new instances of an class

66 Views Asked by At

I have an class:

class Klass:
    pass

and an outside method:

def foo():
    return "foo method"

How to pass the test:

assert Klass().foo() == "foo method"

with decorator for foo method? edited: without changes in Klass

1

There are 1 best solutions below

1
On

I'm not sure I understand what would you like to get by saying decorator but how about this?

class Klass:
    
    def foo(self):
        return "foo method"

assert Klass().foo() == "foo method"

If this is not what are you looking for, please try to find some better words.