Revised PEP 318 - Function/Method Decorator Syntax
Andrew Bennetts
andrew-pythonlist at puzzling.org
Wed Jun 11 04:59:35 EDT 2003
On Wed, Jun 11, 2003 at 11:39:18AM +1000, Andrew Bennetts wrote:
> On Tue, Jun 10, 2003 at 02:28:45PM -0000, Moshe Zadka wrote:
> >
> > Wouldn't it be more readable (:)) if you did
> >
> > class _EvilProperty(type):
> > ....
> > class EvilProperty:
> > __metaclass__ = _EvilProperty
> >
> > class C(object):
> > class x(EvilProperty):
> > def get(self):
> [...etc...]
>
> No, it wouldn't be more readable, because it wouldn't work...
But this would:
----
class _EvilProperty(type):
def __new__(cls, name, bases, d):
if d.get('__metaclass__') == _EvilProperty:
return type.__new__(cls, name, bases, d)
else:
return property(d.get('get'), d.get('set'), d.get('del'),
d.get('__doc__'))
class EvilProperty:
__metaclass__ = _EvilProperty
class C(object):
class x(EvilProperty):
"""An evil test property"""
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__
----
:)
Ta-da! Easy, clean-looking properties, no new syntax required ;)
-Andrew.
More information about the Python-list
mailing list