|
Can anybody explain why the name "Decorator" was chosen for the functionality conveyed by the Decorator design pattern?
I've always found that name fairly misleading, because decorator and marking interface sound very similar to me in their purpose. However...
Started by Uri on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
A decorator "decorates" an interface by adding extra....
Hey, leave it to geeks to make up names.
In essence, you are "decorating" the object with the new functionality.
Because you are adding new functionality to an existing object .
|
|
Why can decorator not decorate a staticmethod or a classmethod?
from decorator import decorator @decorator def print_function_name(function, *args): print '%s was called.' % function.func_name return function(*args) class My_class(object): @print_function...
Answer Snippets (Read the full thread at stackoverflow):
Is this what you wanted?
def print_function_name(function): def wrapper(*args): print('%s was called.' % function.__name__) return function(*args) return wrapper class My_class(object): @classmethod @print_function_name def get_dir(cls): return dir(cls... .
|
|
This question was asked already here , but rather than answering the specific question, descriptions of how the decorator pattern works were given instead. I'd like to ask it again because the answer is not immediately evident to me just by reading how...
Started by es11 on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Using....
For example, say you want your Beverage with milk and nutmeg .
It enables the decoration of the base class independently with various decorators in different combinations without having to derive a class for each possible combination.
|
Ask your Facebook Friends
|
I'm excited to see the latest version of the decorator python module (3.0). It looks a lot cleaner (e.g. the syntax is more sugary than ever) than previous iterations.
However, it seems to have lousy support (e.g. "sour" syntax, to horribly stretch the...
Started by YGA on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
(Anything can be solved by another level of indirection...)
from decorator import decorator def substitute_args(arg_sub_dict): @decorator def wrapper(fun, arg....
In this case, you need to make your function return the decorator.
|
|
When creating decorators for use on class methods, I'm having trouble when the decorator mechanism is a class rather than a function/closure. When the class form is used, my decorator doesn't get treated as a bound method.
Generally I prefer to use the...
Started by Alphabet Soup on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
This how-to guide teaches all you need to know about that!-)
Generally (although not always), when a question is that... .
Your WrapperClass needs to be a descriptor (just like a function is!), i.e., supply appropriate special methods __get__ and __set__ .
|
|
Hi, I'm writing a decorator for methods that must inspect the parent methods (the methods of the same name in the parents of the class in which I'm decorating).
Example (from the fourth example of PEP 318 ):
def returns(rtype): def check_returns(f): def...
Answer Snippets (Read the full thread at stackoverflow):
Class A(object): @returns(int) def compute(self, value): return value * 3
Is the same as saying:
class... .
Here I want to reach the class owning the decorated method f
You can't because at the point of decoration, no class owns the method f.
|
|
Here's an example of what I mean:
class MyDecorator(object): def __call__(self, func): # At which point would I be able to access the decorated method's parent class's instance? # In the below example, I would want to access from here: myinstance def ...
Started by orokusaki on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Class MyDecorator(object): def __call__(self, func): def wrapper(that, *args, **kwargs): ## you can access the "self" of func here through the "that" parameter ## and hence do whatever you want return func(that, *args, **kwargs) return wrapper
Please... .
|
|
I'm implementing a decorator pattern in Javascript.
I've seen the example here in Wikipedia
// Class to be decorated function Coffee() { this.cost = function() { return 1; }; } // Decorator A function Milk(coffee) { this.cost = function() { return coffee...
Started by leeand00 on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
You are clearly interested in programming, and quite skilled, but be aware that talking about a true anything... .
The decorator is supposed to change the way an object behaves, how it does it is purely a matter of implementation.
It does not matter.
|
|
Hi,
can one write sth like:
class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self): pass
This fails: self in @self is unknown
I also tried:
@Test._decorator(self)
which also fails: Test unknown
If would like to temp. change...
Started by hc on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Take, for instance, whether or not the code below looks valid:
class Test(object): def _decorator(self, foo): foo() def bar(self): pass bar = self._decorator(bar)
It, of course, isn't valid since....
What you're wanting to do isn't possible.
|
|
How do I nicely write a decorator?
In particular issues include: compatibility with other decorators, preserving of signatures, ect.
I would like to avoid dependency on the decorator module if possible, but if there were sufficient advantages, then I ...
Started by Casebash on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Print 'Calling ....
Def wrapper(*args, **kwds): ...
@wraps(f) ...
>>> from functools import wraps >>> def my_decorator(f): ...
Directly from the doc.
The signature won't be preserved.
Use functools to preserve the name and doc.
|