Using metaclasses to play with decorators.

Colin J. Williams cjw at sympatico.ca
Thu Jun 24 08:57:49 EDT 2004


David MacQuigg wrote:

> On Wed, 23 Jun 2004 07:30:34 -0400, "Colin J. Williams"
> <cjw at sympatico.ca> wrote:
> 
> 
>>
>>David MacQuigg wrote:
>>
>>>On Sun, 20 Jun 2004 14:48:23 -0400, "Colin J. Williams"
>>><cjw at sympatico.ca> wrote:
>>>
>>>
>>>
>>>>I have yet to wrap my mind around decorators.
>>>
>>>
>>>Decorators are very simple.  They are just a way to provide different
>>>forms of methods, without introducing a new keyword, or some other
>>>more awkward syntax.
>>>
>>>Say you wanted to define a method that didn't have 'self' as its first
>>>argument.  You could add a new keyword to the language:
>>>
>>>noself methodA(x,y):
>>>	return x + y
>>>
>>>Or you could add a "decorator" to the existing syntax:
>>>
>>>def methodA(x,y) [noself]:
>>>	return x + y
>>>
>>>Change 'noself' to 'staticmethod' and you have one of the current
>>>proposals in PEP318.
>>>
>>>Don't get distracted by 'staticmethod' and other mumbo-jumbo
>>>terminology, and you should have no problem with decorators.
>>
>>OK, I'll ignore 'staticmethod', but could you tell me how
>>
>>  def methodA(x, y) [noself]:
>>    return x + y
>>
>>differs in substance from
>>
>>  def methodA(self, y):
>>    return self + y
>>
>>or
>>  def methodA(x, y):
>>    return x + y
>>
>>What has been gained by the added syntactic clutter?
> 
> 
> The example above was to explain decorators, not justify them.  If
> they were to be used *only* to clean up the current syntax for
> staticmethod, then I would say a better alternative would be to get
> rid of the need for a separate staticmethod form entirely. ( See the
> thread "Unification of Methods and Functions" for a thorough
> discussion of this topic.)
> 
> Are decorators on functions necessary?  I can't think of a simpler or
> more consistent way to handle all the variations proposed in PEP 318.
> 
> Assuming that we *will* have a decorator syntax with many options, I
> think that making [staticmethod] one of those options is appropriate.
> I would still prefer a word more meaningful to new users, however. The
> rational I have heard for "staticmethod" is so contorted, it is not
> worth repeating.
> 
> -- Dave
> 
PEP 318 seems to focus on the "how" to implement decorators, rather than 
the "why".  Is there some accessible explanation of the purpose of them?

Colin W.




More information about the Python-list mailing list