python-decorators Questions

1

I wrote a unit test for main_function and asserted that it calls the function get_things inside it with an instance of a class, mocked with patch as a parameter: @patch("get_something") @patch("My...
Zincograph asked 12/4, 2018 at 13:57

2

The following two property definitions show up exactly the same in Sphinx autodoc HTML output: @property def concrete(self): """This is the concrete docstring""" pass @abstractproperty def abst...
Streeter asked 24/7, 2013 at 17:52

1

Solved

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 j...
Joannejoannes asked 9/2, 2023 at 22:11

1

Solved

I have a class decorator, which adds a few functions and fields to decorated class. @mydecorator @dataclass class A: a: str = "" Added (via setattr()) is a .save() function and a set of...
Walford asked 11/1, 2023 at 14:57

2

Solved

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 element...
Raycher asked 16/12, 2022 at 16:36

5

Solved

In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn't work. If I do this: import abc cl...

8

Solved

I'm trying to create a custom decorator in Django but I couldn't find any ways to do it. # "views.py" @custom_decorator def my_view(request): # ....... So, how can I create it in Djan...
Amoroso asked 29/3, 2011 at 7:23

7

Solved

Apologies this is a very broad question. The code below is a fragment of something found on the web. The key thing I am interested in is the line beginning @protected - I am wondering what th...
Vindicable asked 21/8, 2012 at 0:10

1

When I ran the code below with @asyncio.coroutine decorator on Python 3.11.0: import asyncio @asyncio.coroutine # Here def test(): print("Test") asyncio.run(test()) I got the error be...

3

Solved

What is the main difference between the two? I have been studying Python and came across them. A decorator is essentially a function that wraps another function and you can do anything before and a...
Underfeed asked 12/6, 2018 at 18:21

14

Solved

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function ...

4

Solved

I am attempting to integrate a very old system and a newer system at work. The best I can do is to utilize an RSS firehouse type feed the system utilizes. The goal is to use this RSS feed to make t...
Singles asked 13/6, 2016 at 11:55

5

I have a simple decorator to track the runtime of a function call: def timed(f): def caller(*args): start = time.time() res = f(*args) end = time.time() return res, end - start return caller...
Quadrennial asked 9/11, 2018 at 19:32

2

Solved

Here's a simplified function for which I'm trying to add a lru_cache for - from functools import lru_cache, wraps @lru_cache(maxsize=1000) def validate_token(token): if token % 3: return None r...
Spohr asked 18/2, 2022 at 14:41

1

Solved

I want to write a function decorator that removes itself from the stack trace when an exception occurs (outside the logic specific to the decorator itself), for example when: the caller uses argum...
Impi asked 6/5, 2022 at 19:20

3

Solved

I want numba to be an optional dependency, so that it's fast if installed or slow if not. So when numba is not installed, I want @njit to be a dummy decorator that does nothing. If I follow these ...
Shakhty asked 3/9, 2019 at 15:12

2

Solved

I want to have classes that automatically send notifications to subscribers whenever one of their attributes change. So if I would write this code: @ChangeMonitor class ChangingClass(object): de...
Pavla asked 15/9, 2013 at 12:26

5

Solved

I have inherited some code that implements pytest.mark.skipif for a few tests. Reading through the pytest docs, I am aware that I can add conditions, possibly check for environment variables, or us...
Admiration asked 10/5, 2019 at 13:20

1

Solved

Some uncontroversial background experimentation up front: import inspect def func(foo, bar): pass print(inspect.signature(func)) # Prints "(foo, bar)" like you'd expect def decorator(...
Wigfall asked 14/5, 2022 at 0:13

4

Solved

Consider the following code: from typing import Callable, Any TFunc = Callable[..., Any] def get_authenticated_user(): return "John" def require_auth() -> Callable[TFunc, TFunc]: d...
Estranged asked 1/11, 2017 at 17:8

2

Solved

def make_bold(fn): return lambda : "<b>" + fn() + "</b>" def make_italic(fn): return lambda : "<i>" + fn() + "</i>" @make_bold @make_italic def hello(): return "hello w...
Offense asked 7/12, 2014 at 11:24

8

Solved

How do I pass a class field to a decorator on a class method as an argument? What I want to do is something like: class Client(object): def __init__(self, url): self.url = url @check_authoriza...
Pearcy asked 30/7, 2012 at 23:30

2

Solved

First of all, I understand how, in general, a decorator work. And I know @staticmethod strips off the instance argument in the signature, making class C(object): @staticmethod def foo(): print ...

1

Solved

As a Python programmer, I frequently declare classes similar to class Foo: def __init__(self, attr1, attr2, attr3, attr4, attr5, attr6, attr7, attr8, attr9): self.attr1 = attr1 self.attr2 = attr...
Neoplasm asked 19/2, 2022 at 21:29

5

Solved

I'm refreshing my memory about some python features that I didn't get yet, I'm learning from this python tutorial and there's an example that I don't fully understand. It's about a decorator counti...
Elgon asked 7/7, 2017 at 10:1

© 2022 - 2024 — McMap. All rights reserved.