decorator

Using Python property() inside a method

痴心易碎 提交于 2020-01-22 16:46:26
问题 Assuming you know about Python builtin property: http://docs.python.org/library/functions.html#property I want to re-set a object property in this way but, I need to do it inside a method to be able to pass to it some arguments, currently all the web examples of property() are defining the property outside the methods, and trying the obvious... def alpha(self, beta): self.x = property(beta) ...seems not to work, I'm glad if you can show me my concept error or other alternative solutions

Using Python property() inside a method

柔情痞子 提交于 2020-01-22 16:46:03
问题 Assuming you know about Python builtin property: http://docs.python.org/library/functions.html#property I want to re-set a object property in this way but, I need to do it inside a method to be able to pass to it some arguments, currently all the web examples of property() are defining the property outside the methods, and trying the obvious... def alpha(self, beta): self.x = property(beta) ...seems not to work, I'm glad if you can show me my concept error or other alternative solutions

How would one decorate an inherited method in the child class?

余生长醉 提交于 2020-01-22 14:12:08
问题 I'm not quite sure how to use a decorator on an inherited method. Normally decorators are put before the definition but for an inherited function the definition is given in the parent and not the child class. I would like to avoid rewriting the definition of the method in the child class and simply specify to put the decorator around the inherited method. To make myself clearer, here is a working example of what I mean: class Person(): def __init__(self, age): self.age = age @classmethod def

How would one decorate an inherited method in the child class?

Deadly 提交于 2020-01-22 14:11:35
问题 I'm not quite sure how to use a decorator on an inherited method. Normally decorators are put before the definition but for an inherited function the definition is given in the parent and not the child class. I would like to avoid rewriting the definition of the method in the child class and simply specify to put the decorator around the inherited method. To make myself clearer, here is a working example of what I mean: class Person(): def __init__(self, age): self.age = age @classmethod def

Can someone explain how the source code of staticmethod works in python

懵懂的女人 提交于 2020-01-22 13:23:07
问题 First of all, I understand how, in general, a decorator work. And I know @staticmethod strips off the instance argument in the signature, making class C(object): @staticmethod def foo(): print 'foo' C.foo //<function foo at 0x10efd4050> C().foo //<function foo at 0x10efd4050> valid. However, I don't understand how the sourcec code of staticmethod make this happen. It seems to me that when wrapping method foo in staticmethod , an instance of staticmethod is instantiated, then some magic

Explanation of a decorator class in python

邮差的信 提交于 2020-01-22 10:02:14
问题 While reading up about some python module I encountered this decorator class: # this decorator lets me use methods as both static and instance methods class omnimethod(object): def __init__(self, func): self.func = func def __get__(self, instance, owner): return functools.partial(self.func, instance) What I know about decorators, is that the can extend functionality (e.g. for a function). Could someone be so kind and explain to me why the class above is useful and how it exactly works ? It is

Differences between Proxy and Decorator Pattern

こ雲淡風輕ζ 提交于 2020-01-19 06:28:25
问题 Can you give any good explanation what is the difference between Proxy and Decorator ? The main difference I see is that when we assume that Proxy uses composition and Decorator uses aggregation then it seems to be clear that by using multiple (one or more) Decorators you can modify/ add functionalities to pre-existing instance (decorate), whereas Proxy has own inner instance of proxied class and delegates to it adding some additional features (proxy behaviour). The question is - Does Proxy

How to convert recursive “divide and conquer” function to a dynamic programming function using decorators?

浪子不回头ぞ 提交于 2020-01-16 18:54:37
问题 I am trying to write a decorator function which converts a pure recursive function with two arguments that uses a "divide and conquer" strategy to an equivalent but more efficient one using dynamic programming. Note: it is designated to decorate two input functions. So I am trying to memoize the values but I am not sure how to correctly implement it in the form of a decorator? Also how can it decorate two input functions? EDIT: This is what I have managed to do: profile_results = {} t = {} ''

How to convert recursive “divide and conquer” function to a dynamic programming function using decorators?

邮差的信 提交于 2020-01-16 18:53:13
问题 I am trying to write a decorator function which converts a pure recursive function with two arguments that uses a "divide and conquer" strategy to an equivalent but more efficient one using dynamic programming. Note: it is designated to decorate two input functions. So I am trying to memoize the values but I am not sure how to correctly implement it in the form of a decorator? Also how can it decorate two input functions? EDIT: This is what I have managed to do: profile_results = {} t = {} ''

How to convert recursive “divide and conquer” function to a dynamic programming function using decorators?

谁都会走 提交于 2020-01-16 18:53:05
问题 I am trying to write a decorator function which converts a pure recursive function with two arguments that uses a "divide and conquer" strategy to an equivalent but more efficient one using dynamic programming. Note: it is designated to decorate two input functions. So I am trying to memoize the values but I am not sure how to correctly implement it in the form of a decorator? Also how can it decorate two input functions? EDIT: This is what I have managed to do: profile_results = {} t = {} ''