关于python中@property装饰器解释
#普通方法和加属性后的对比
"""
class Foo:
def func(self):
print(123)
return 666
obj = Foo()
res = obj.func()
print(res)
class Foo:
@property
def func(self):
print(123)
return 666
obj = Foo()
res = obj.func
#注意点:
#我们在方法前面添加了属性,我们可以直接调用其方法名。
#方法不可以有self之外的参数
print(res)
"""
属性装饰器应用:
实现分页功能:
#属性的应用
class Page:
def __init__(self, total_count, current_page, page_count=10):
"""
:param total_count: 总的数据数量
:param current_page: 当前要查看的页码
:param page_count: 每一页显示的数量,默认值10,每一页显示10条数据
"""
self.total_count = total_count
self.page_count = page_count
self.current_page = current_page
@property
def start_index(self):
return (self.current_page - 1) * self.page_count
@property
def end_index(self):
return (self.current_page) * self.page_count
USER_LIST = []
for item in range(321):
data_list = USER_LIST.append("alex-%s"%(item,))
#实现分页功能:
current_page= int(input("输入请要查看的页码:"))
obj = Page(321, current_page)
data_list = USER_LIST[obj.start_index:obj.end_index]
for i in data_list:
print("打印当前页下所有数据:", i)
if __name__ == "__main__":
pass