Reputation: 9146
I want to create property method dynamically.
It means, for example, i want first class A to be equivalent with second one.
Is there any way?
class A(object):
def __init__(self):
self._a = 10
self.createPropertyMethod(a, self._a, getFunc, setFunc, delFunc)
def createPropertyMethod(self, name, value, getFunc, setFunc, delFunc)
#TODO
.
class A(object):
def __init__(self):
self._a = 10
@property
def a(self):
return getFunc(self._a)
@a.setter
def a(self, value):
setFunc(self._a, value)
@a.deleter
def a(self):
delFunc(self._a)
Upvotes: 1
Views: 91
Reputation: 1123560
You can't, not on instances. The property
object needs to be part of the class for the descriptor protocol on which it relies to work.
You can use the __getattr__
, __setattr__
and __delattr__
hooks instead to proxy attribute access dynamically.
Upvotes: 3