Decorating class methods – how to pass the instance to the decorator?

You need to make the decorator into a descriptor — either by ensuring its (meta)class has a __get__ method, or, way simpler, by using a decorator function instead of a decorator class (since functions are already descriptors). E.g.: def dec_check(f): def deco(self): print ‘In deco’ f(self) return deco class bar(object): @dec_check def foo(self): print ‘in … Read more

Python functools lru_cache with instance methods: release object

This is not the cleanest solution, but it’s entirely transparent to the programmer: import functools import weakref def memoized_method(*lru_args, **lru_kwargs): def decorator(func): @functools.wraps(func) def wrapped_func(self, *args, **kwargs): # We’re storing the wrapped method inside the instance. If we had # a strong reference to self the instance would never die. self_weak = weakref.ref(self) @functools.wraps(func) @functools.lru_cache(*lru_args, … Read more

How do I pass extra arguments to a Python decorator?

Since you are calling the decorator like a function, it needs to return another function which is the actual decorator: def my_decorator(param): def actual_decorator(func): print(“Decorating function {}, with parameter {}”.format(func.__name__, param)) return function_wrapper(func) # assume we defined a wrapper somewhere return actual_decorator The outer function will be given any arguments you pass explicitly, and should … Read more

Decorator execution order

Decorators wrap the function they are decorating. So make_bold decorated the result of the make_italic decorator, which decorated the hello function. The @decorator syntax is really just syntactic sugar; the following: @decorator def decorated_function(): # … is really executed as: def decorated_function(): # … decorated_function = decorator(decorated_function) replacing the original decorated_function object with whatever decorator() … Read more

@classmethod vs @staticmethod in Python

Maybe a bit of example code will help: Notice the difference in the call signatures of foo, class_foo and static_foo: class A(object): def foo(self, x): print(f”executing foo({self}, {x})”) @classmethod def class_foo(cls, x): print(f”executing class_foo({cls}, {x})”) @staticmethod def static_foo(x): print(f”executing static_foo({x})”) a = A() Below is the usual way an object instance calls a method. The … Read more

Real world example about how to use property feature in python?

Other examples would be validation/filtering of the set attributes (forcing them to be in bounds or acceptable) and lazy evaluation of complex or rapidly changing terms. Complex calculation hidden behind an attribute: class PDB_Calculator(object): … @property def protein_folding_angle(self): # number crunching, remote server calls, etc # all results in an angle set in ‘some_angle’ # … Read more

Class method decorator with self arguments?

Yes. Instead of passing in the instance attribute at class definition time, check it at runtime: def check_authorization(f): def wrapper(*args): print args[0].url return f(*args) return wrapper class Client(object): def __init__(self, url): self.url = url @check_authorization def get(self): print ‘get’ >>> Client(‘http://www.google.com’).get() http://www.google.com get The decorator intercepts the method arguments; the first argument is the instance, … Read more

How does the @property decorator work in Python?

The property() function returns a special descriptor object: >>> property() <property object at 0x10ff07940> It is this object that has extra methods: >>> property().getter <built-in method getter of property object at 0x10ff07998> >>> property().setter <built-in method setter of property object at 0x10ff07940> >>> property().deleter <built-in method deleter of property object at 0x10ff07998> These act as … Read more

How do I make function decorators and chain them together?

If you are not into long explanations, see Paolo Bergantino’s answer. Decorator Basics Python’s functions are objects To understand decorators, you must first understand that functions are objects in Python. This has important consequences. Let’s see why with a simple example : def shout(word=”yes”): return word.capitalize()+”!” print(shout()) # outputs : ‘Yes!’ # As an object, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)