python-decorators Questions

3

I'm updating some code to use Dash and plotly. The main code for graphing is defined within a class. I replaced some Bokeh widgets with Dash controls, and ended up with a callback that looks like t...
Cleancut asked 17/2, 2019 at 2:15

3

Solved

I want to have a function that can use functools.lru_cache, but not by default. I am looking for a way to use a function parameter that can be used to disable the lru_cache. Currently, I have a two...
Cosper asked 11/6, 2019 at 12:59

1

Recently, with the change of the @classmethod decorator no longer being able to wrap the @property decorator (Python >= 3.11), there has been significant interest in how to create a @classproper...

4

Solved

I'm trying to write an abstract class with unimplemented methods, which will force the inheriting children to return a value of a specific type when they override the method (defined in the decorat...
Sheffield asked 23/12, 2019 at 18:10

1

Well-defined custom exceptions are often more informative than builtin ones; e.g. AgeError more so than ValueError. So in general I try to use the former when I can. But as a consequence of this, m...
Meath asked 6/2, 2016 at 4:28

2

In Python 3.9, we gained the ability to chain @classmethod and @property to sensibly create class properties. class Foo: @property def instance_property(self): return "A regular property&qu...

9

Solved

My problem, and why I'm trying to write a decorator for a method, @cachedproperty. I want it to behave so that when the method is first called, the method is replaced with its return value. I also ...
Dittany asked 18/4, 2016 at 1:55

3

Solved

I wanted to print the source code for my_func, that is wrapped by my_decorator: import inspect from functools import wraps def my_decorator(some_function): @wraps(some_function) def wrapper(): ...
Headrest asked 19/4, 2017 at 21:28

5

Solved

I want to add an auth_required decorator to my endpoints. (Please consider that this question is about decorators, not middleware) So a simple decorator looks like this: def auth_required(func): d...
Scherzo asked 23/10, 2020 at 9:40

6

Solved

I have a decorator like below. def myDecorator(test_func): return callSomeWrapper(test_func) def callSomeWrapper(test_func): return test_func @myDecorator def someFunc(): print 'hello' I want...
Disordered asked 16/4, 2012 at 14:38

7

I am trying to use click python package to pass a command line argument to a function. The example from official documentation works as explained. But nowhere in the documentation is it mentioned h...
Foremast asked 7/10, 2014 at 23:19

4

Solved

I am trying to build a control structure in a class method that takes a function as input and has different behaviors if a function is decorated or not. Any ideas on how you would go about building...
Barbour asked 29/7, 2021 at 23:40

4

If I create a decorator like the following: def my_decorator(some_fun): def wrapper(): print("before some_fun() is called.") some_fun() print("after some_fun() is called.") ...
Toscana asked 26/7, 2017 at 19:12

2

Solved

I want to, for debugging purposes, print out something pertaining to each and every line executed in a python method. For example if there was some assignment in the line, i want to print what va...
Merrymaker asked 23/8, 2015 at 5:19

22

Solved

How do I make two decorators in Python that would do the following? @make_bold @make_italic def say(): return "Hello" Calling say() should return: "<b><i>Hello</i&gt...
Owl asked 11/4, 2009 at 7:5

2

To wrap all function calls of a module and access it via an wrapper class's __getattr__ method, I tried to use typing library but I could not figure out on how to do this correctly. import in...
Hypertensive asked 17/4, 2019 at 8:5

10

Solved

How can I use functools.lru_cache inside classes without leaking memory? In the following minimal example the foo instance won't be released although going out of scope and having no referrer (othe...
Wisteria asked 12/11, 2015 at 13:21

2

Solved

How do you make an abstract property in python? import abc class MyClass(abc.ABC): @abc.abstractmethod @property def foo(self): pass results in the error AttributeError: attribute '__isabstrac...
Longlegged asked 23/6, 2022 at 21:28

3

Solved

Consider this small example: import datetime as dt class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): start = dt.datetime.now() ret = self.func(*...
Vivie asked 7/5, 2015 at 14:29

2

Could somebody please clarify the generally accepted way of defining a property as part of a Protocol? I've been doing it like so: from typing import Protocol class MyProtocol(Protocol): @propert...
Monroe asked 2/10, 2021 at 13:58

2

Solved

Why can't I make a class' __call__ method static using the @staticmethod decorator? class Foo(object): @staticmethod def bar(): return 'bar' @staticmethod def __call__(): return '__call__' ...
Throttle asked 7/11, 2014 at 3:32

36

Solved

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Expensive asked 25/9, 2008 at 21:1

1

The doc says about @register.filter below: Custom filters are Python functions that take one or two arguments: The value of the variable (input) – not necessarily a string. The value of the argum...

1

Solved

I was trying to come up with a use case for the new @enum.nonmember decorator in Python 3.11. The docs clearly mention it is a decorator meant to be applied to members. However, when I tried litera...
Doenitz asked 11/5, 2023 at 20:20

5

Solved

Is it possible, and if so, advisable, and if so, what would be the recommended method for decorating a function that yields a value? For example, consider this imaginary example I made up def foo...
Preventive asked 23/11, 2014 at 10:14

© 2022 - 2024 — McMap. All rights reserved.