Re: Using decorators with argument in Python

2011-07-01 Thread John Posner
On 2:59 PM, Ethan Furman wrote: snip def __call__(self, func=None): if func is None: return self._call() self.func = func return self def _call(self): print(\n + self.char * 50) self.func() print(self.char * 50 + '\n')

Re: Using decorators with argument in Python

2011-06-30 Thread Duncan Booth
Lie Ryan lie.1...@gmail.com wrote: Simplicity is one, using @decor() means you have at least three-level nested functions, which means the code is likely to be very huge and perhaps unnecessarily. If you don't like the extra level of function nesting that you get from returning a decorator

Re: Using decorators with argument in Python

2011-06-29 Thread jigar tanna
yes for this case you will have to use @memoize() as all the arguments are optional ... Thanks, J --- On Tue, 28/6/11, Ian Kelly ian.g.ke...@gmail.com wrote: From: Ian Kelly ian.g.ke...@gmail.com Subject: Re: Using decorators with argument in Python To: Jigar Tanna poisonousratt...@gmail.com

Re: Using decorators with argument in Python

2011-06-29 Thread jigar tanna
: Using decorators with argument in Python To: python-list@python.org Date: Tuesday, 28 June, 2011, 10:36 PM On 06/29/2011 02:52 AM, Jigar Tanna wrote: coming across to certain views from people, it is not a good practice to use decorators with arguments (i.e. @memoize() ) and instead it is good

Re: Using decorators with argument in Python

2011-06-29 Thread John Posner
On 2:59 PM, Lie Ryan wrote: Can any of you guys explain me advantages and disadvantages of using each of them Simplicity is one, using @decor() means you have at least three-level nested functions, which means the code is likely to be very huge and perhaps unnecessarily. Bruce Eckel pointed

Re: Using decorators with argument in Python

2011-06-29 Thread Ethan Furman
John Posner wrote: Investigating how this fact fit in with the current thread, I came up with an alternative to the three levels of def (pronounced three levels of death). Following is code for two decorators: * the first one encloses the output of a function with lines of # characters, and is

Re: Using decorators with argument in Python

2011-06-29 Thread Ian Kelly
On Wed, Jun 29, 2011 at 1:30 PM, Ethan Furman et...@stoneleaf.us wrote: How about just having one bit of code that works either way? How would you adapt that code if you wanted to be able to decorate a function that takes arguments? This also won't work if the argument to the decorator is

Re: Using decorators with argument in Python

2011-06-29 Thread Ethan Furman
Ian Kelly wrote: On Wed, Jun 29, 2011 at 1:30 PM, Ethan Furman et...@stoneleaf.us wrote: How about just having one bit of code that works either way? How would you adapt that code if you wanted to be able to decorate a function that takes arguments?

Re: Using decorators with argument in Python

2011-06-29 Thread Ian Kelly
On Wed, Jun 29, 2011 at 3:29 PM, Ethan Furman et...@stoneleaf.us wrote: 8 class enclose(object):    func = None    def __init__(self, char='#'):        self.char = char        if callable(char):  # was a function passed in

Re: Using decorators with argument in Python

2011-06-29 Thread Ethan Furman
Ian Kelly wrote: @enclose def test5(string, func): print(func(string)) test5('broken', func=str.upper) Yes, that is a limitation -- one loses the func keyword for the decorated function. If I were to actually use this, I'd probably go with '_func' as the keyword. ~Ethan~ PS Thanks

Using decorators with argument in Python

2011-06-28 Thread Jigar Tanna
I am new to Python and Django, was going through the concept of decorators where I came across a special case of using arguments with decorators Below is the code for memoization where I was looking at the concept... cache = {} def get_key(function, *args, **kw) : key = '%s. %s: ' %

Re: Using decorators with argument in Python

2011-06-28 Thread Lie Ryan
On 06/29/2011 02:52 AM, Jigar Tanna wrote: coming across to certain views from people, it is not a good practice to use decorators with arguments (i.e. @memoize() ) and instead it is good to just use @memoize. Can any of you guys explain me advantages and disadvantages of using each of

Re: Using decorators with argument in Python

2011-06-28 Thread Ian Kelly
On Tue, Jun 28, 2011 at 10:52 AM, Jigar Tanna poisonousratt...@gmail.com wrote: coming across to certain views from people, it is not a good practice to use decorators with arguments (i.e. @memoize() ) and instead it is good to just use @memoize. Can any of you guys explain me advantages and

Re: Using decorators with argument in Python

2011-06-28 Thread Ben Finney
Jigar Tanna poisonousratt...@gmail.com writes: where I came across a special case of using arguments with decorators A decorator is a function which takes exactly one parameter, and returns a function based on that parameter. URL:http://docs.python.org/glossary.html#term-decorator