Method decoration in python

I create a small wrapper module for a public library, there are a lot of repetitions in the library, where after creating the object, perhaps the methods require the same data elements.

I need to pass the same data in my wrapper class, but I don’t really want to pass the same thing over and over again. Therefore, I would like to save the data in my wrapper class and apply it if it is not included in this method. However, if something goes along the road, I want the method arguments to overwrite the standard defaults. Here is a code snippet that illustrates my goals.

class Stackoverflow(): def __init__(self,**kwargs): self.gen_args = {} #Optionally add the repeated element to the object if 'index' in kwargs: self.gen_args['index'] = kwargs['index'] if 'doc_type' in kwargs: self.gen_args['doc_type'] = kwargs['doc_type'] #This is where the problem is def gen_args(fn): def inner(self,*args,**kwargs): kwargs.update(self.gen_args) return fn(*args,**kwargs) return inner #There is a bunch of these do_stuffs that require index and doc_type @gen_args def do_stuff(self,**kwargs): print(kwargs['index']) print(kwargs['doc_type']) #Just send arguments up with the method print("CASE A") a = Stackoverflow() a.do_stuff(index=1,doc_type=2) #Add them to the class and have all methods use them without having to specify #them each time print("CASE B") b = Stackoverflow(index=1,doc_type=2) b.do_stuff() #The arguments specified in the method should overwrite class values print("CASE C") c = Stackoverflow(index=1,doc_type=2) c.do_stuff(index=3,doc_type=4) 

EDIT:

So the question is, how do I fix gen_args or is there a better way to do this? The specific error I get with this code is: return fn (* args, ** kwargs) TypeError: do_stuff () missing 1 required positional argument: 'self'

+6
source share
1 answer

I can use this inner definition:

  def inner(self,*args,**kwargs): return fn(self, *args,**dict(self.gen_args, **kwargs)) 

Notes:

  • This version provides self , which is not available in your version.
  • This gives priority to the transmitted kwargs .
+5
source

Source: https://habr.com/ru/post/985731/


All Articles