Python 类变动的钩子方法 属性方法
Python 类变动的钩子方法
from typing import List
class Field:
def __init__(self, name, is_user=False):
self.name = name
self.is_user = is_user
def __str__(self):
return f'name={self.name},is_user={self.is_user}'
class FieldQueue:
def __init__(self, not_born_queue=False):
self.q: List[Field] = []
self.exist_origin_name = {}
self.not_born_queue = not_born_queue
def append(self, a: Field):
self.q.append(a)
def __str__(self):
return f'size={len(self.q)}'
@property
def user_queue(self):
if self.not_born_queue:
return
q = FieldQueue(not_born_queue=False)
for i in self.q:
if i.is_user:
q.append(i)
return q
@property
def event_queue(self):
if self.not_born_queue:
return
q = FieldQueue(not_born_queue=True)
for i in self.q:
if not i.is_user:
q.append(i)
return q
lst: List[Field] = []
for i in (('a', True), ('b', True), ('c', False), ('d', False)):
lst.append(Field(i[0], i[1]))
case = FieldQueue()
for f in lst:
case.append(f)
Python属性和方法 https://segmentfault.com/a/1190000044061768