Revised PEP 318 - Function/Method Decorator Syntax

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Jun 10 15:49:23 CEST 2003


On Tue, Jun 10, 2003 at 12:25:13PM +0000, Kevin Smith wrote:
[...]
>     The proposed syntax is general enough that it could be used 
>     on class definitions as well as shown below.
> 
>         class foo(object) as classmodifier:
>             class definition here
> 
>     However, there are no obvious parallels for use with other
>     descriptors such as property().

Note that you can already do tricky stuff with properties if you really want
to define one all at once:

    class EvilProperty(type):
        def __new__(cls, name, bases, d):
            return property(d.get('get'), d.get('set'), d.get('del'), d['__doc__'])
    
    class C(object):
        class x:
            """An evil test property"""
            __metaclass__ = EvilProperty
            def get(self):
                print 'Getting'
                return 1
            def set(self, value):
                print 'Setting to', value
                
    c = C()
    print c.x
    c.x = 5  
    print C.x.__doc__

This has the advantage that you can define your property in a single block,
and you don't have to look too far ahead in the nested class to see the
crucial "__metaclass__ = EvilProperty" line.

It does kinda feel like yet-another-gratuitous-use-of-metaclasses, though ;)

-Andrew.






More information about the Python-list mailing list