python分页处理


 1 # @Time     :2019/6/17 16:00
 2 # -*- encoding:utf-8 -*-
 3 
 4 data_list = []
 5 for i in range(1, 901):
 6     data_list.append('alex-%s' % i)
 7 
 8 
 9 class Page:
10     '''
11     处理分页相关的代码
12     '''
13 
14     def __init__(self, page, per_page_num=10):
15         '''
16         数据初始化一个900条数据的列表
17         :param page: 要查看的页码
18         :param per_page_num: 默认每页显示10条
19         '''
20         self.page = page
21         self.per_page_num = per_page_num
22 
23     @property
24     def start(self):
25         return (self.page - 1) * self.per_page_num
26 
27     @property
28     def end(self):
29         return self.page * self.per_page_num
30 
31     ###################写法一.###################
32     # while True:
33     #     # 1. 要查看的页面
34     #     page = int(input('请输入要查看的页码:'))
35     #
36     #     # 2. 每页显示 10 条
37     #     #per_page_num=10
38     #     obj = Page(page)
39     #     page_data_list = data_list[obj.start:obj.end]
40     #     for item in page_data_list:
41     #         print(item)
42 
43     ###################写法二.###################
44 
45     def run(self, start, end):
46         page_data_list = data_list[start:end]
47         for item in page_data_list:
48             print(item)
49 
50 
51 while True:
52     page = int(input('请输入要查看的页码:'))
53     obj = Page(page)
54     obj.run(obj.start, obj.end)