python-decorators

Why does my context manager not exit on exception

Why does my context manager not exit on exception Question: I am learning about context managers and was trying to build one myself. The following is a dummy context manager that opens a file in read mode (I know I can just do with open(…): …. this is just an example I built to help …

Total answers: 1

How to add decorator to dynamically create class

How to add decorator to dynamically create class Question: I want to convert this code to be dynamic: @external_decorator class Robot: counter = 0 def __init__(self, name): self.name = name def sayHello(self): return "Hi, I am " + self.name I can create the class dynamically, this way: def Rob_init(self, name): self.name = name Robot2 = …

Total answers: 1

How to have typing support for a static property (using a decorator)

How to have typing support for a static property (using a decorator) Question: Given a static property decorator: class static_property: def __init__(self, getter): self.__getter = getter def __get__(self, obj, objtype): return self.__getter(objtype) @staticmethod def __call__(getter_fn): return static_property(getter_fn) That is applied to a class as follows: class Foo: @static_prop def bar(self) -> int: return 10 Add …

Total answers: 1

AtributeError: can't set attribute for python list property

AtributeError: can't set attribute for python list property Question: I’m working with the python-docx library from a forked version, and I’m having an issue with editing the elements list as it is defined as a property. # docx.document.Document @property def elements(self): return self._body.elements I tried to go with the solution mentioned here but the error …

Total answers: 2

does the @property decorator function as a getter?

does the @property decorator function as a getter? Question: i am new to python and i’m trying to understand the use of the ‘getter’. it’s use case is not obvious to me. if i use a property decorator on a method and im able to return a certain value, what exactly would i use ‘getter’ …

Total answers: 2

How do I tell pylint about a descriptor providing access to iterables, subscriptables?

How do I tell pylint about a descriptor providing access to iterables, subscriptables? Question: I have a decorator that I can use to mark a read-only class property: class class_ro_property(property): def __init__(self, getter:Callable): self._getter = getter def __get__(self, _, cls): return self._getter(cls) class MyClass: @class_ro_property def my_property(cls): return [1, 2, 3] The problem is that …

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

Function does not return value when used with decorators

Function does not return value when used with decorators Question: When I call the function without the decorator a list of all files with the matching extension is returned. However, if I add the decorator @get_time, I do not get a return value from the function. def get_time(func): from time import perf_counter from functools import …

Total answers: 2