@decorator syntax is sugar, but for what exactly?

xtian xtian at toysinabag.com
Mon Aug 9 05:49:10 CEST 2004

Avner Ben <avner at skilldesign.com> wrote in message 
> The "property" call resembles both classmethod, staticmethod and 
> instancemethod, but cannot be eliminated using the new function 
> decorator syntax, because of its m:1 nature  - one property binds 
> together a getter, a setter etc., where staticmethod etc. change the 
> status of one function in one way.

> Talking about properties, I like the C# way of defining them, which is 
> straightforward and readable. The property begins like a method, but has 
> no argument list and includes a getter function with no arguments and a 
> setter function with one argument. Adapted to Python, it would look 
> something like:
> class hasProperty:
> 	def __init__(self,aProperty='')
> 		self.aProperty = aProperty
> 	def AProperty:
> 		def get(self):
> 			return self.aProperty
> 		def set(self,value):
> 			self.aProperty = value
> obj = hasProperty()
> obj.AProperty = 'test'
> print obj.AProperty

I'm not sure that this application of the new syntax is much worse
than what you've got... (it *is* a bit hacky in that it's calling the
function it's wrapping, but there you go).

>>> def property_(f):
	return property(*f())

>>> class DecorationTest(object):
	def __init__(self):
		self._foo = 1
	def foo():
		def get(self):
			print "get"
			return self._foo
		def set(self, val):
			print "set"
			self._foo = val
		return get, set

>>> t = DecorationTest()
>>> t.foo
>>> t.foo = 3
>>> t.foo

What do people think of something like this?


More information about the Python-list mailing list