我创建了一个具有多个属性的类。我想将一个函数用于 getter 方法,将第二个函数用于 setter 方法。
class person:
def __init__(self, fname, lname, city, state):
# make all attributes as private
self._fname = fname
self._lname = lname
self._city = city
self._state = state
@property # get method
def fname(self):
return self._fname
@fname.setter # set method
def fname(self,fname):
self._fname = fname
@property
def lname(self):
return self._lname
@lname.setter
def lname(self,lname):
self._lname = lname
@property
def city(self):
return self._city
@city.setter
def city(self, city):
self._city = city
@property
def state(self):
return self._state
@state.setter
def state(self, state):
self._state = state
如何将所有属性用于一种 get 方法和一种 set 方法?
例如:
def get(self):
return self._attr
def set(self,value):
self._attr = value