decorator

Python can't use setter

Python can't use setter Question: here is my issue, when i’m using @property decorator I can’t use setter class Worker: def __init__(self,name): self.__name = name @property def name(self): return self.__name @name.setter def set_name(self,new_name): self.__name = new_name worker1 = Worker(‘A’) print(worker1.name) worker1.name = ‘B’ print(worker1.name) It gives AttributeError: can’t set attribute ‘name’, when I use setter …

Total answers: 2

how i can change my function into decorator python?

how i can change my function into decorator python? Question: in this code i checking the email and password validation if email ends with {@gmail.com} and password length is 8 i print (hello user) def login(email, password): valid_mail = "@gmail.com" print() if email[-10:] == valid_mail and len(str(password)) == 8: print(f’hello {email} welcome back’) else: print("invalid …

Total answers: 2

Python decorator for backoff retries in class

Python decorator for backoff retries in class Question: I have a class named Client which tries to connect to a server via Websockets. I tried to make a decorator to retry if it couldn’t connect to the server but I get the error ValueError: a coroutine was expected, got <__main__.Client object at 0x000001BE86D71930> The code …

Total answers: 1

Flask not executing function when wrapped in a decorator

Flask not executing function when wrapped in a decorator Question: I have an api based on flask. I am using also the multiprocessing module which may be involved in the following issue. When using a non trivial wrapper the flask app just does not execute the wrapped task. There is also no error message und …

Total answers: 1

Python decorator within function, needs class within method?

Python decorator within function, needs class within method? Question: Going off of How can I define decorator method inside class? and Accessing self within decorator function within a Python class, I have the following code: class Custom(): def __init__(self, var): self.var = var def renew(func): @wraps(func) def wrapper(self, *args, **kwargs): try: return func(*args, **kwargs) except: …

Total answers: 1

How to call a decorator (instance method) of other class?

How to call a decorator (instance method) of other class? Question: I have a class called RMath() which takes 2 values a and b. Then there is a function named add() which sums up these a and b. Now i want to record the calculation time. I decided to use decorator here without touching the …

Total answers: 3

python decorator to check for already called func with unique arguments

python decorator to check for already called func with unique arguments Question: I m writing python decorator to check if func was previously called with same arguments. Below is proof of concept code storage = list() def f(*args, **kwargs): s = ” for i in range(len(args)): s += str(args[i]) kv = ” for k, v …

Total answers: 1

How do I prevent returning None between desired output?

How do I prevent returning None between desired output? Question: I am trying to return the function with arguments and the functions results in the format of the print statement. The code works except I am getting a "None" between each answer when a test is run. How do I prevent the None from printing? …

Total answers: 1

Chaining decorators result is confusing me

Chaining decorators result is confusing me Question: def decor1(func): def inner(): x = func() return x * x return inner def decor(func): def inner(): x = func() return 2 * x return inner @decor1 @decor def num(): return 10 print(num()) Can someone explain why the result is 400? Is it because inner is being returned …

Total answers: 2