L
Laszlo Zsolt Nagy
My problem is about properties and the virtuality of the methods. I
would like to create a property whose get and set methods
are virtual. I had the same problems in Delphi before and the solution
was the same. I created a private _get method and a public
get method. The former one will call the latter. The same stands for
_set and set. Here is an example below:
class C1(object):
def namechanged(self):
"""Called after the name of the component has been changed."""
pass
def getname(self):
"""Returns the name of the component.
You can override this method to provide generated component
names."""
return self._name
def setname(self,name):
"""Set the name of the component.
You can override this method to programatically control
component name changes.
"""
self._name = name
self.namechanged()
def _getname(self):
"""Internal method, do not use"""
return self.getname()
def _setname(self,name):
"""Internal method, do not use"""
self.setname(name)
name = property(_getname,_setname,"Name")
class C2(C1):
def getname(self):
return "lala"
class C3(C1):
def _getname(self):
return "lala"
name = property(_getname,None,"Name")
c1 = C1()
c2 = C2()
c3 = C3()
c1.name = 'Test1'
c2.name = 'Test2'
c3.name = 'Test3'
print c1.name # This will print 'Test1'
print c2.name # This will print 'lala'
print c3.name # This will print 'lala'
print C3.name is C1.name # False
I cannot override C2._getname instead, because c2.name would print
'Test2" instead of lala. Clearly, the property stores a reference to the
get and set methods and it is not possible to have it use the new
methods. Creating a new property is the worst - need to duplicate code
and also C3.name is C1.name returns False. It is not a big problem
because I found the solution just I wonder if there is a better way to
"virtualize" property get/set functions.
--
_________________________________________________________________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: (e-mail address removed)
Python forever!
would like to create a property whose get and set methods
are virtual. I had the same problems in Delphi before and the solution
was the same. I created a private _get method and a public
get method. The former one will call the latter. The same stands for
_set and set. Here is an example below:
class C1(object):
def namechanged(self):
"""Called after the name of the component has been changed."""
pass
def getname(self):
"""Returns the name of the component.
You can override this method to provide generated component
names."""
return self._name
def setname(self,name):
"""Set the name of the component.
You can override this method to programatically control
component name changes.
"""
self._name = name
self.namechanged()
def _getname(self):
"""Internal method, do not use"""
return self.getname()
def _setname(self,name):
"""Internal method, do not use"""
self.setname(name)
name = property(_getname,_setname,"Name")
class C2(C1):
def getname(self):
return "lala"
class C3(C1):
def _getname(self):
return "lala"
name = property(_getname,None,"Name")
c1 = C1()
c2 = C2()
c3 = C3()
c1.name = 'Test1'
c2.name = 'Test2'
c3.name = 'Test3'
print c1.name # This will print 'Test1'
print c2.name # This will print 'lala'
print c3.name # This will print 'lala'
print C3.name is C1.name # False
I cannot override C2._getname instead, because c2.name would print
'Test2" instead of lala. Clearly, the property stores a reference to the
get and set methods and it is not possible to have it use the new
methods. Creating a new property is the worst - need to duplicate code
and also C3.name is C1.name returns False. It is not a big problem
because I found the solution just I wonder if there is a better way to
"virtualize" property get/set functions.
--
_________________________________________________________________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: (e-mail address removed)
Python forever!