How to get self into a Python method without explicitly accepting it

后端 未结 5 990
失恋的感觉
失恋的感觉 2020-12-09 12:53

I\'m developing a documentation testing framework -- basically unit tests for PDFs. Tests are (decorated) methods of instances of classes defined by the framework, and these

5条回答
  •  失恋的感觉
    2020-12-09 13:35

    This might be a use case for decorators - you give them a small set of lego bricks to build functions with, and the complicated framework stuff is piped in via @testcase or somesuch.

    Edit: You didn't post any code, so this is going to be sketchy, but they don't need to write methods. They can write ordinary functions without "self", and you could use decorators like in this example from the article I linked:

    class myDecorator(object):
    
        def __init__(self, f):
            print "inside myDecorator.__init__()"
            f() # Prove that function definition has completed
    
        def __call__(self):
            print "inside myDecorator.__call__()"
    
    @myDecorator
    def aFunction():
        print "inside aFunction()"
    

提交回复
热议问题