Revised PEP 318 - Function/Method Decorator Syntax
Michele Simionato
mis6 at pitt.edu
Tue Jun 10 13:06:04 EDT 2003
Andrew Bennetts <andrew-pythonlist at puzzling.org> wrote in message news:<mailman.1055253020.26604.python-list at python.org>...
> 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.
Carl Banks posted something similar few days ago. Evil is a good name,
since x returns a property instance and not a class. I think he wrote
something like this:
class C(object):
class x:
"""An evil test property"""
def __metaclass__(name, bases, d):
return property(d.get('get'), d.get('set'),
d.get('del'),d.get('__doc__'))
def get(self):
print 'Getting'
return 1
def set(self, value):
print 'Setting to', value
I hope nobody will ever try this in production code!
Michele
More information about the Python-list
mailing list