程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

8、 Python learning notes - object oriented - Exercise

編輯:Python

Class

# Implement assignment , Delete
class Foo:
def __init__(self, name):
self.name = name
@property # Declared as an attribute of a class
def bar(self):
return self.name
@bar.setter
def bar(self, val):
print(' This field has been reset ')
self.name = val
@bar.deleter
def bar(self):
print(' This field has been deleted ')
self.name = ''
foo = Foo('name')
r = foo.bar
print(r) # Print self.name, The value is passed in the past name
foo.bar = ' Reassign ' # Reassign , take self.name The assignment is hello, Print ' This field has been reset
r = foo.bar
print(r)
del foo.bar # Delete assignment , take self.name Assign to null ( If the assignment is null, it will return ), Of course, it can also be deleted (del self.name), Only after deletion , The program will report an error if it cannot get the return value , Because it is the variable, not the value of the variable, that is deleted . Print ' This field has been deleted
r = foo.bar
print(r)

Pagination

# altogether 100000 A content , Each page shows 10 individual , Display the corresponding content according to the entered page number
# Create a content list
li = []
for i in range(1, 100000):
li.append(i)
""" Define classes and methods
1、 Construction method , Pass the page number entered by the user , Pass to instance variables self.page
2、 Start of calculation , Return the starting value through the attribute of the class , You can use a variable like calling method when calling later ( Do not use parentheses when calling )
3、 Calculate the end content , Return the end value through the attribute of the class , You can use a variable like calling method when calling later ( Do not use parentheses when calling )
notes : Each page shows 10 A content , Note that the list values include the left and not the right ,li The first value in the list is 1
first page 1-10 li[0,10]
The second page 11-20 li[10,20]
The third page 21-30 li[20,30]
...
start = (page-1) * 10
end = page *10
"""
class Pergination:
def __init__(self, current_page):
try:
p = int(current_page)
except Exception as e:
print(e)
current_page = 1
self.page = int(current_page)
@property
def start(self):
start_page = (self.page - 1) * 10
return start_page
@property
def end(self):
end_page = (self.page) * 10
return end_page
""" Get user input page number , Return the corresponding content
1、input Get page number
2、 Instantiate the class Pergination, Transmittal page number
3、 Call a class method to return data
"""
while True:
current_page = input(' Please enter the page number :')
page = Pergination(current_page)
print(li[page.start:page.end])

Use special class members to implement slicing

# Simulation list slice
"""
1、slice class ,python A kind of , Format [i:j:k],i Is the starting value ,j Is the end value ,k It's stepping ( How many values are taken ), Yes ‘:’ It is commonly slice type
2、slice Like start,stop,step, Corresponding [i:j:k] Medium i,j,k.
3、 If it is a slicing operation, it must be slice class , Whether it is a slice is determined by the type of the passed in parameter
"""
class Foo:
def __getitem__(self, item):
if type(item) == slice:
print(' Users use slicing operations ')
print(item.start)
print(item.stop)
print(item.step)
return ' section '
else:
print(' Users use the index to get values ')
return ' Indexes '
bar = Foo()
print(bar[1])
print(bar[1:3:2])

Simulate a web page access request at the terminal

# With reflection, you can do this in simple code , If you don't use reflection, you need to use a lot of if Judge
class Web:
@staticmethod
def f1():
return ' home page '
@staticmethod
def f2():
return ' Journalism '
@staticmethod
def f3():
return ' music '
inp = input(' Please enter the request page :')
try:
r = getattr(Web, inp)
except Exception as e:
print('404 The page doesn't exist ')
else:
print(r())
""" If you use if
if inp == 'f1':
print(web.f1())
elif inp == 'f2':
print(web.f2())
...
"""

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved