python-decorators

Doctest and Decorators in Python

不羁的心 提交于 2021-02-07 03:53:16
问题 I was trying to use Python decorator to catch exceptions and log the exceptions. import os.path import shutil class log(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): print "Inside __call__()" try: self.f(*args) except Exception: print "Sorry" @log def testit(a, b, c): print a,b,c raise RuntimeError() if __name__ == "__main__": testit(1,2,3) It works fine Desktop> python deco.py Inside __init__() Inside __call__() 1 2 3 Sorry The issue is that

Doctest and Decorators in Python

强颜欢笑 提交于 2021-02-07 03:51:14
问题 I was trying to use Python decorator to catch exceptions and log the exceptions. import os.path import shutil class log(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): print "Inside __call__()" try: self.f(*args) except Exception: print "Sorry" @log def testit(a, b, c): print a,b,c raise RuntimeError() if __name__ == "__main__": testit(1,2,3) It works fine Desktop> python deco.py Inside __init__() Inside __call__() 1 2 3 Sorry The issue is that

How to freeze some arguments over multiple related class methods

為{幸葍}努か 提交于 2021-01-28 11:29:49
问题 What's the "best" method to take a collection of functions that use some common argument names (assumed to mean the same thing), and make an object that holds these functions, but with some key argument values fixed, or at least their defaults fixed. If I'm going to have a set of functions meant to work on some specific data defined by a set of attributes, I'd usually use a class, providing the attributes that must be set in the __init__ . But sometimes it makes more sense to start with

Objects having same name refer to different id in python

强颜欢笑 提交于 2021-01-28 09:18:53
问题 In the below code snippet, two objects named div are created at lines 1 and 2. How does python differentiate between the two div objects created under the same scope? When id() is applied on both objects, two different addresses are shown for the similar named objects. Why is this so? def div(a,b): return a/b print(id(div)) # id = 199......1640 ################################ line 1 def smart_div(func): def inner(a,b): if a<b: a,b=b,a return func(a,b) return inner a = int(input("Enter 1st: "

Objects having same name refer to different id in python

↘锁芯ラ 提交于 2021-01-28 09:10:23
问题 In the below code snippet, two objects named div are created at lines 1 and 2. How does python differentiate between the two div objects created under the same scope? When id() is applied on both objects, two different addresses are shown for the similar named objects. Why is this so? def div(a,b): return a/b print(id(div)) # id = 199......1640 ################################ line 1 def smart_div(func): def inner(a,b): if a<b: a,b=b,a return func(a,b) return inner a = int(input("Enter 1st: "

Python: Calling a function based on a argument value

一个人想着一个人 提交于 2021-01-28 08:45:47
问题 From my main code, I want to call a function X regardless of argument v. In the background, the function Y or Z is called based on the value of v. For example, main code is - i = X(v) Now, functions Y or Z are called if v="a" or v="b". def X(v): pass def Y(v): # called if v="a" def Z(v): # called if v="b" I think a decorator can be used but I don't have enough knowledge about decorators. 回答1: The easiest way is to have X call Y or Z . X would then be more of a dispatcher instead of a

Python: __qualname__ of function with decorator

若如初见. 提交于 2021-01-28 08:40:50
问题 I'm using a Decorator (class) in an Instance method of another class, like this: class decorator_with_arguments(object): def __init__(self, arg1=0, arg2=0, arg3=0): self.arg1 = arg1 self.arg2 = arg2 self.arg3 = arg3 def __call__(self, f): print("Inside __call__()") def wrapped_f(*args): print(f.__qualname__) f(*args) return wrapped_f class Lol: @decorator_with_arguments("hello") def sayHello(self,a1, a2, a3, a4): print(self.sayHello.__qualname__) Now, when I print out self.sayHello.__qualname

Apply Flask MethodView decorator just for some methods

南笙酒味 提交于 2021-01-28 07:12:01
问题 I was wondering if there is any way that I can use a decorator just for some methods from my class, for example in the following code I want token_required to decorate all methods excepting the POST, how could I possibly achieve that? class UserAPI(MethodView): def token_required(view_method): @wraps(view_method) def decorated(*args, **kwargs): token = None if 'token' in request.headers: token = request.headers['token'] if not token: return "no token" return view_method(*args, **kwargs)

Python @property.setter

亡梦爱人 提交于 2021-01-27 22:14:27
问题 The basic way of creating decorators is def my_decorator(f): def _f(*args, **kwargs): # do something using f pass return _f @my_decorator def f(...): ... But that way you cannot define decorators like @property.setter , because the name of the property (and thus the name of the decorator) is different every time. How is it @property.setter defined then? Is it possible to do something similar in Python, or is it built-in feature available only from C (implementation) level? 回答1: What you are

python decorators, nested function [duplicate]

霸气de小男生 提交于 2021-01-27 14:33:39
问题 This question already has answers here : How to make function decorators and chain them together? (17 answers) Closed 5 years ago . I'm trying to figure out why i need a one more nested function when using decorators. Here is an example: def func(f): def deco(*args, **kwargs): return f(*args, **kwargs) return deco @func def sum(a, b): return a+b print sum(5, 10) Code works, everything is fine. But why do i need to create nested "deco" function? Let's try without it: def func(f): return f(