如何使用python为列表中的每个元素设置@property?

时间:2018-05-08 03:46:25

标签: python class

我正在创建一个存储time.Say

的类
all_lists = [A, B, C]

[[i-j for i,j in zip(subtract_from,l)] for l in all_lists]

[
  [734, 3821, 734, 3821, 555],
  [1134, 4221, 694, 3771, 4355],
  [694, 4021, 934, 4221, 5545]
]

我想限制class TimePoint(object): def __init__(self): # a time list to store year, month, day, hour, minute, second self.time_unit = [-1, -1, -1, -1, -1, -1] 中每个元素的范围,例如self.time_unit,这意味着0 < self.time_unit[1] <= 12范围为1~12。所以,我想使用{{1 to decorator month的每个元素.Say

@property

但是,self.time_unitimport arrow class TimePoint(object): def __init__(self): self.time_unit = [-1, -1, -1, -1, -1, -1] self.oor = False time_now = arrow.now() self.year = time_now.year self.month = time_now.month self.day = time_now.day self.hour = time_now.hour self.minute = time_now.minute self.second = time_now.second @property def t_unit(self): return self.time_unit @t_unit.setter def t_unit(self, value): if value < 0 or value > 9999: self.time_unit[0] = self.year self.oor = True if self.time_unit[0] < 0 or self.time_unit[0] > 9999: self.time_unit[0] = self.year self.oor = True if self.time_unit[1] < 0 or self.time_unit[1] > 12: self.time_unit[1] = self.month self.oor = True if self.time_unit[2] < 0 or self.time_unit[2] > 31: self.time_unit[2] = self.day self.oor = True if self.time_unit[3] < 0 or self.time_unit[3] > 24: self.time_unit[3] = self.hour self.oor = True if self.time_unit[4] < 0 or self.time_unit[4] > 60: self.time_unit[4] = self.minute self.oor = True if self.time_unit[5] < 0 or self.time_unit[5] > 60: self.time_unit[5] = self.second self.oor = True 只是装饰整个列表@property,而不是@t_unit.setter中的元素。那么,如何装饰self.time_unit的元素?我应该为每个元素写self.time_unit吗?

self.time_unit

0 个答案:

没有答案