import math
class Pointer(object):
def __init__(self,x,y):
self.x = x
self.y = y
class Circle(object):
def __init__(self,cp,radius):
self.cp = cp
self.radius = radius
def get_area(self):
return self.radius ** 2 * math.pi
def get_length(self):
return self.radius * 2 * math.pi
def relationship(self,point):
"""
判断点到圆的距离 点和圆心的距离和半径对比
"""
distance = math.sqrt((point.x - self.cp.x) ** 2 + (point.y - self.cp.y) ** 2)
if distance > self.radius:
print('点在圆外')
elif distance < self.radius:
print('点在圆内')
else:
print('点在圆上')
p = Pointer(3,4)
c = Circle(p,5)
print(c.get_area())
print(c.get_length())
p1 = Pointer(10,10)
c.relationship(p1)
p2 = Pointer(0,0)
c.relationship(p2)
class PetShop(object):
def __init__(self,shop_name,pet_list=None):
self.shop_name = shop_name
if pet_list is None:
pet_list = []
self.pet_list = pet_list
def show_pets(self):
if len(self.pet_list) == 0:
print('本店还没有宠物')
return
print('{}有{}宠物'.format(self.shop_name,len(self.pet_list)))
for pet in self.pet_list:
print(pet)
class Pet(object):
def __init__(self,name,gender,age,breed):
self.name = name
self.gender = gender
self.age = age
self.breed = breed
def bark(self):
print(self.name + '正在叫')
def eat(self):
print(self.name + '正在吃')
def __str__(self):
return '姓名:{},性别:{},品种:{},年龄:{}'.format(self.name,self.gender,self.breed,self.age)
class PetDog(Pet):
def __init__(self,name,gender,age,breed):
super(PetDog,self).__init__(name,gender,age,breed)
def bark(self):
print(self.name + '正在汪汪汪汪')
def bulid_home(self):
print(self.name + '正在拆家')
def eat(self):
print(self.name + '正在啃骨头')
class PetCat(Pet):
def __init__(self,name,gender,age,breed,eyes_color):
super(PetCat,self).__init__(name,gender,age,breed)
self.eyes_color = eyes_color
def bark(self):
print(self.name + '正在喵喵喵')
def sajiao(self):
print(self.name + '正在撒娇')
def eat(self):
print(self.name + '正在吃鱼')
def __str__(self):
x = super(PetCat,self).__str__()
x += ',眼色:{}'.format(self.eyes_color)
return x
ps1 = PetShop('萌宠')
ps1.show_pets()
dog1 = PetDog('大黄','female',3,'哈士奇')
dog2 = PetDog('二黄','male',2,'萨摩耶')
cat1 = PetCat('tom','male',2,'英短','blue')
cat2 = PetCat('tom2','male',22,'英短','blue')
ps2 = PetShop('萌宠',[dog1,dog2,cat1,cat2])
ps2.show_pets()
class Student(object):
def __init__(self,number,name,age,gender,score):
self.number = number
self.name = name
self.age = age
self.gender = gender
self.score = score
def __str__(self):
return '编号:{},姓名:{},性别:{},分数:{}'.format(self.number,self.name,self.gender,self.score)
class Grade(object):
def __init__(self,name,students):
self.name = name
self.students = students
def show_all(self):
for student in self.students:
print(student)
def failed_students(self):
result = filter(lambda student:student.score < 60,self.students)
for student in result:
print(student)
def order_students(self):
return sorted(self.students,key=lambda s:s.score,reverse=True)
def get_students_by_number(self,n):
for s in self.students:
if s.number == n:
return s
s1 = Student(1,'张三',18,'male',90)
s2 = Student(2,'李四',15,'male',56)
s3 = Student(3,'王五',16,'male',45)
s4 = Student(4,'麻子',24,'male',89)
s5 = Student(5,'小明',22,'male',70)
g = Grade('中二班',[s1,s2,s3,s4,s5])
g.show_all()
g.failed_students()
x = g.order_students()
for i in x:
print(i)
y = g.get_students_by_number(4)
print(y)
def demo(a,b):
try:
x = a / b
except ZeroDivisionError:
return '除数不能为零'
else:
return x
finally:
print('1234')
print(demo(1,0))
try:
file = open('../sds','r',encoding='utf8')
except FileNotFoundError:
print('文件未找到')
else:
try:
file.read()
finally:
file.close()
try:
with open('../sdf','r',encoding='utf8') as file:
file.read()
except FileNotFoundError:
print('文件未找到')
class Demo(object):
def __enter__(self):
pass
def __exit__(self,exc_type,exc_val,exc_tb):
pass
def create_obj():
return Demo()
with create_obj() as d:
print(d)
class MyError(Exception):
def __init__(self,x,y):
self.x = x
self.y = y
def __str__(self):
return '密码长度在{}和{}之间'.format(self.x,self.y)
def can_play(fn):
def inner(name,game,**kwargs):
clock = kwargs.get('clock',21)
if clock >= 21:
print('太晚了不能玩')
else:
fn(name,game)
return inner
@can_play
def play_game(name,game):
print(name + '在玩' + game)
play_game('张三','王者',clock=20)
def can_play(clock):
def handle_action(fn):
def do_action(name,game):
if clock < 21:
fn(name,game)
else:
print('太晚了不能玩')
return do_action
return handle_action
@can_play(20)
def play_game(name,game):
print(name + '在玩' + game)
play_game('张三','王者')
user_permission = 9
read_permission = 4
write_permission = 2
exe_permission = 1
del_permission = 8
def check_permission(x,y):
def handle_action(fn):
def do_action():
if x & y != 0:
fn()
else:
print('对不起,您没有相应的权限')
return do_action
return handle_action
@check_permission(user_permission,read_permission)
def read():
print('读取')
@check_permission(user_permission,write_permission)
def write():
print('写入')
@check_permission(user_permission,exe_permission)
def execute():
print('执行')
@check_permission(user_permission,del_permission)
def delete():
print('删除')
read()
write()
execute()
delete()
from collections.abc import Iterable
class Foo():
def __next__(self):
return 1
class Demo(object):
def __init__(self,x):
self.x = x
self.count = 0
def __iter__(self):
return self
def __next__(self):
self.count += 1
if self.count <= self.x:
return self.count - 1
else:
raise StopIteration
d = Demo(10)
names = list(('zhangsan','lisi'))
print(isinstance(d,Iterable))
print(isinstance(names,Iterable))
for i in d:
print(i)
for i in range(10):
print(i)
class Demo(object):
def __init__(self,x):
self.x = x
self.count = 0
def __iter__(self):
return self
def __next__(self):
self.count += 1
if self.count <= self.x:
return self.count - 1
else:
raise StopIteration
d = Demo(10)
print(d.__iter__().__next__())
i = iter(d)
print(next(i))
class Fibonacci(object):
def __init__(self,n):
self.n = n
self.num1 = self.num2 = 1
self.count = 1
def __iter__(self):
return self
def __next__(self):
self.count += 1
if self.count <= self.n:
self.num1,self.num2 = self.num2,self.num1 + self.num2
return self.num1
else:
raise StopIteration
f = Fibonacci(11)
for i in f:
print(i)
num1 = [1,23,4,5,6,7,8,9,0,0,0,21]
转载请注明原文地址:https://blackberry.8miu.com/read-32737.html