C# style properties in python
Posted
by 3D-Grabber
on Stack Overflow
See other posts from Stack Overflow
or by 3D-Grabber
Published on 2010-05-15T19:21:29Z
Indexed on
2010/05/15
19:24 UTC
Read the original article
Hit count: 215
I am looking for a way to define properties in Python similar to C#, with nested get/set definitions.
This is how far I got:
#### definition ####
def Prop(fcn):
f = fcn()
return property(f['get'], f['set'])
#### test ####
class Example(object):
@Prop
def myattr():
def get(self):
return self._value
def set(self, value):
self._value = value
return locals() # <- how to get rid of this?
e = Example()
e.myattr = 'somevalue'
print e.myattr
The problem with this is, that it still needs the definition to 'return locals()'.
Is there a way to get rid of it?
Maybe with a nested decorator?
© Stack Overflow or respective owner