Python decorator? – can someone please explain this? [duplicate]

Take a good look at this enormous answer/novel. It’s one of the best explanations I’ve come across. The shortest explanation that I can give is that decorators wrap your function in another function that returns a function. This code, for example: @decorate def foo(a): print a would be equivalent to this code if you remove … Read more

Builder Vs Decorator pattern [closed]

From wikipedia’s decorator pattern article: In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically. There’s no need to add toppings to a Pizza after it has been fully constructed. You don’t eat half a pizza and then add another topping to it. … Read more

Is it possible to decorate include(…) in django urls with login_required?

It is doable, and in fact I just found two snippets for this. Solution #1 The first snippet by cotton substitutes RegexURLPattern and RegexURLResolver with custom implementations that inject given decorator during resolve call. from django.core.urlresolvers import RegexURLPattern, RegexURLResolver from django.conf.urls.defaults import patterns, url, include from django.contrib import admin from myproject.myapp.decorators import superuser_required class DecoratedURLPattern(RegexURLPattern): … Read more

Python Class Based Decorator with parameters that can decorate a method or a function

You don’t need to mess around with descriptors. It’s enough to create a wrapper function inside the __call__() method and return it. Standard Python functions can always act as either a method or a function, depending on context: class MyDecorator(object): def __init__(self, argument): self.arg = argument def __call__(self, fn): @functools.wraps(fn) def decorated(*args, **kwargs): print “In … Read more

What is the best way to do automatic attribute assignment in Python, and is it a good idea?

There are some things about the autoassign code that bug me (mostly stylistic, but one more serious problem): autoassign does not assign an ‘args’ attribute: class Foo(object): @autoassign def __init__(self,a,b,c=False,*args): pass a=Foo(‘IBM’,’/tmp’,True, 100, 101) print(a.args) # AttributeError: ‘Foo’ object has no attribute ‘args’ autoassign acts like a decorator. But autoassign(*argnames) calls a function which returns … Read more

Multithreading for Python Django

I’ve continued using this implementation at scale and in production with no issues. Decorator definition: def start_new_thread(function): def decorator(*args, **kwargs): t = Thread(target = function, args=args, kwargs=kwargs) t.daemon = True t.start() return decorator Example usage: @start_new_thread def foo(): #do stuff Over time, the stack has updated and transitioned without fail. Originally Python 2.4.7, Django 1.4, … Read more

Why can @decorator not decorate a staticmethod or a classmethod?

classmethod and staticmethod return descriptor objects, not functions. Most decorators are not designed to accept descriptors. Normally, then, you must apply classmethod and staticmethod last when using multiple decorators. And since decorators are applied in “bottom up” order, classmethod and staticmethod normally should be top-most in your source. Like this: class My_class(object): @classmethod @print_function_name def … Read more

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