decorator

Clear list inside decorator from global scope

Clear list inside decorator from global scope Question: def outer(func): a = [] def inner(): func() a.append(‘work’) print(a) return inner @outer def test(): print(‘TEST’) test() test() # *1 test() # *2 *1 here I want clear list a inside a decorator *2 so here must be a new list a inside outer Is it posible …

Total answers: 3

How can I dynamically extend arguments of a method?

How can I dynamically extend arguments of a method? Question: The methods should be callable by assigning the specific parameters as well as considering the class’ attributes. So what I like to achieve is overwriting a method’s arguments with preset attributes. E.g.: class Obj: def __init__(self, cfg=None): self.cfg = {} if cfg: self.cfg = cfg …

Total answers: 2

Changing class attributes in decorator

Changing class attributes in decorator Question: I’m using django as my framework for some web application. I implemented a modelview of my own because I have a few querysets and seriazliers in the same view. For this use, I needed to implement all of the CRUD functions myself: class Models1AndModel2View(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, GenericViewSet): …

Total answers: 1

decorator argument: unable to access instance attribute

decorator argument: unable to access instance attribute Question: Context class MyMockClient below has two methods: push_log: pushes one log from list self.logs and pops it push_event: pushes one event from list self.events and pops it Both methods are decorated using __iterate_pushes which essentially iterate push_event and push_log until their respective lists are entirely popped AND …

Total answers: 1

Automatically add methods to a class in Python

Automatically add methods to a class in Python Question: I’m trying to create a custom class to handle timeseries related to various objects. They all inherit from a base class called Timeseries, which looks like this class Timeseries: def __init__(self, t_max: int): self.t_max = t_max self.current_t = 0 def update(self): if self.current_t == self.t_max: raise …

Total answers: 1

Automatically add decorator to all inherited methods

Automatically add decorator to all inherited methods Question: I want in class B to automatically add the decorator _preCheck to all methods that have been inherited from class A. In the example b.double(5) is correctly called with the wrapper. I want to avoid to manually re-declare (override) the inherited methods in B but instead, automatically …

Total answers: 2

Error occuring when I try to run decorator with @ – Python

Error occuring when I try to run decorator with @ – Python Question: I am having a problem with the following programm. When I try to run decorator the easier way, using @ like this def decorator1(fun): def wrapper(): text = ‘——‘ return text + ‘n’ + fun + ‘n’ + text return wrapper() def …

Total answers: 2

Count how many times each function gets called

Count how many times each function gets called Question: I want to count how many times each function get called. I have a wrapper to do the counting and save it into a global variable def counter(f): global function_calls function_calls = 0 def wrapper(*args, **kwargs): global function_calls function_calls += 1 return f(*args, **kwargs) return wrapper …

Total answers: 2

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called Question: I’m trying to add a __set_name__ hook to the descriptor produced by functools.wraps inside a decorator, but it is never called and I don’t see any error messages: import functools def wrap(fn): """Decorator.""" @functools.wraps(fn) def w(*args, **kwargs): return fn(*args, **kwargs) # This never gets …

Total answers: 1