每天进步一点点-python如何实现链式调用
class Query:
"""想要重复的调用相同方法,需要返回对象本身,并将查询的条件收集起来,最后统一查询"""
def __init__(self):
self.query_condition = {}
def filter(self, **kwargs):
self.query_condition.update(kwargs)
return self
query = Query()
a = query.filter(name="china").filter(age=30, sex="male")
print(a.query_condition)
"""
{'name': 'china', 'age': 30, 'sex': 'male'}
"""
人生苦短,我用python!