考慮下面的類定義的Python:多個屬性,一個二傳手/吸氣
class of2010(object):
def __init__(self):
self._a = 1
self._b = 2
self._c = 3
def set_a(self,value):
print('setting a...')
self._a = value
def set_b(self,value):
print('setting b...')
self._b = value
def set_c(self,value):
print('setting c...')
self._c = value
a = property(fset=self.set_a)
b = property(fset=self.set_b)
c = property(fset=self.set_c)
注意set_[a|b|c]()
做同樣的事情。有沒有辦法做定義:
def set_magic(self,value):
print('setting <???>...')
self._??? = value
一次使用它的A,B,C如下
a = property(fset=self.set_magic)
b = property(fset=self.set_magic)
c = property(fset=self.set_magic)
它是財產(fset = ...),但否則我會使用類似的東西。 – olt 2010-01-23 15:20:29
@olt:謝謝,修復。 – 2010-01-23 15:22:56