Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jan Kaliszewski
21-08-2009 o 18:09:02 alex23 wrote: Unfortunately, apply() has been removed as a built-in in 3.x. You can always implement it yourself :) def apply(function, args=(), keywords={}): return function(*args, **keywords) -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/lis

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine
Steven D'Aprano wrote: On Sat, 22 Aug 2009 10:51:27 +0100, Jonathan Fine wrote: Steven D'Aprano wrote: There's a standard idiom for that, using the property() built-in, for Python 2.6 or better. Here's an example including a getter, setter, deleter and doc string, with no namespace pollution

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 10:51:27 +0100, Jonathan Fine wrote: > Steven D'Aprano wrote: > >> There's a standard idiom for that, using the property() built-in, for >> Python 2.6 or better. >> >> Here's an example including a getter, setter, deleter and doc string, >> with no namespace pollution, import

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine
Steven D'Aprano wrote: There's a standard idiom for that, using the property() built-in, for Python 2.6 or better. Here's an example including a getter, setter, deleter and doc string, with no namespace pollution, imports, or helper functions or deprecated built-ins: class ColourThing(obje

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Steven D'Aprano
On Sat, 22 Aug 2009 08:09:52 +0100, Jonathan Fine wrote: > Jonathan Gardner wrote: >> On Aug 21, 9:09 am, alex23 wrote: >>> On Aug 21, 11:36 pm, Jonathan Fine wrote: >>> >>> class ColourThing(object): >>> @apply >>> def rgb(): >>> def fset(self, rgb): >>> self.r, self

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Leonhard Vogt
Why I've personally stopped using it: I've always had the impression that decorators were intended to provide a convenient and obvious way of augmenting functions. Having one that automatically executes the function at definition just runs counter to the behaviour I expect from a decorator. Espe

Re: Using 'apply' as a decorator, to define constants

2009-08-22 Thread Jonathan Fine
Jonathan Gardner wrote: On Aug 21, 9:09 am, alex23 wrote: On Aug 21, 11:36 pm, Jonathan Fine wrote: class ColourThing(object): @apply def rgb(): def fset(self, rgb): self.r, self.g, self.b = rgb def fget(self): return (self.r, self.g, self.b)

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread Steven D'Aprano
On Fri, 21 Aug 2009 15:17:40 -0700, Jonathan Gardner wrote: >> Unfortunately, apply() has been removed as a built-in in 3.x. I'm not >> sure if it has been relocated to a module somewhere, there's no mention >> of such in the docs. > > apply = lambda f: f() > > It's one of those functions th

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread alex23
Jonathan Gardner wrote: > This is brilliant. I am going to use this more often. I've all but > given up on property() since defining "get_foo", "get_bar", etc... has > been a pain and polluted the namespace. Unfortunately I can't remember who I first learned it from - it was definitely in a post

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread Jonathan Gardner
On Aug 21, 9:09 am, alex23 wrote: > On Aug 21, 11:36 pm, Jonathan Fine wrote: > > class ColourThing(object): >     @apply >     def rgb(): >         def fset(self, rgb): >             self.r, self.g, self.b = rgb >         def fget(self): >             return (self.r, self.g, self.b) >         re

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread Jonathan Gardner
On Aug 21, 6:36 am, Jonathan Fine wrote: >     �...@apply >      def tags(): >          value = [] >          # complicated code >          return value > Is this different from: tags = [] # complicated code I can see the argument that you are cleaning up a lot of intermediary variables upon re

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread Jonathan Fine
alex23 wrote: Unfortunately, apply() has been removed as a built-in in 3.x. I'm not sure if it has been relocated to a module somewhere, there's no mention of such in the docs. The old use of apply() You can save yourself the tidy up by using the same name for the function & the label:

Re: Using 'apply' as a decorator, to define constants

2009-08-21 Thread alex23
On Aug 21, 11:36 pm, Jonathan Fine wrote: > It might seem odd to use 'apply' as a decorator, but it can make sense. Yes, it's an idiom I've used myself for property declarations, but one I find myself using less often: class ColourThing(object): @apply def rgb(): def fset(self, r