[Python-Dev] PEP 318: Properties (original) (raw)
Phillip J. Eby pje at telecommunity.com
Sat Apr 3 11:32:45 EST 2004
- Previous message: [Python-Dev] PEP 318: Properties
- Next message: [Python-Dev] PEP 318: Properties
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
At 06:15 PM 4/3/04 +0200, Martin v. Löwis wrote:
Guido van Rossum wrote:
I'm still torn whether to promote defining properties this way: [propget] def x(self): "Doc string for x" _return self.x [propset] def x(self, newx): _self.x = newx [propdel] def x(self): _del self.x but if people like this (whatever the decorator syntax :) we might as well make this the recommended way to define properties. Does that actually work? I.e. is there an implementation of propget, propset, propdel so that this code introduces a property x?
Yes, using sys._getframe().f_locals[function.func_name]. Someone posted a link to an implementation earlier this week.
My understanding is that above syntax would be short for> [propget] def x(self): "Doc string for x" _return self.x x = propget(x)
def x(self, newx): _self.x = newx x = propset(x) def x(self): _del self.x x = propdel(x) Later assignments to x would override earlier ones, so that only the propdel survives.
Technically, what you show is not the actual expansion of the new syntax. The new syntax applies decorators before binding 'x' to the new function. So, the old value of 'x' is available to a decorator via sys._getframe().f_locals. This technique is also useful for implementing generic functions and/or multimethods, signature-based overloading, etc.
- Previous message: [Python-Dev] PEP 318: Properties
- Next message: [Python-Dev] PEP 318: Properties
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]