补充基础
- 列表生成式
# values = []
# for x in range(1,101):
# values.append(x)
#
# print(values)
# #列表生成式
# value = [x for x in range(1,101)]
# print(value)
#取1-100之间的偶数
# values = []
# for x in range(1,100):
# if x % 2 == 0:
# values.append(x)
# print(values)
value = [x for x in range(1,101) if x % 2 == 0]
print(value)
- 模仿三目运算符
a = 5
b = 2
c = None
if a > b:
c = 1
else:
c = 0
c = 1 if a>b else 0
- property装饰器
'''
有些时候你给一个类的某个属性设置变量的时候,可能除了设置变量,还要做一些其它的事情。
或者你在读取某个值的时候,想在返回值之前做点其他的事情,那么你可以使用property装饰器来完成。
比如拿打飞机游戏来讲,在飞机这个类中,有一个属性alive用来保存这个飞机是否还活着,
如果设置为False,那么即飞机已经死亡,这时候除了保存这个状态,还要执行中弹的动画。
这时候就可以使用property装饰器来完成了
'''
class Plane(object):
def __init__(self):
self._alive = True
self.score = 0
#将alive方法设置为属性,然后以后调用temp = p.alive,就会执行这个方法
@property
def alive(self):
if not self._alive:
self.cancel_schedule()
return self._alive
@alive.setter
def alive(self,value):
self._alive = value
if value == False:
self.die_action()
def cancel_schedule(self):
print('取消事件调度')
@property
def score(self):
return self._score
@score.setter
def score(self,value):
self._score = value
self._update_score_brand(value)
def _update_score_brand(self,value):
print('积分榜的值为:%d' % value)
def die_action(self):
print('飞机被撞状态')
p = Plane()
hit = True
if hit:
# p.set_alive(False)
# p.alive = False
temp = p.alive
p.set_score(100)
# p.get_alive()
# p.set_score(100)
#要去更改积分榜的值
- 实例:面向对象的宠物管理系统
class Pet(object):
"""
宠物类
"""
def __init__(self,pet_id,pet_name,pet_type,pet_price):
self.id = pet_id
self.name = pet_name
self.type = pet_type
self.price = pet_price
@classmethod
def pet_with_line(cls,line):
pet_id,pet_name,pet_type,pet_price = line.replace('\n','').split('&')
pet = Pet(pet_id,pet_name,pet_type,pet_price)
return pet
def get_line(self):
return "{id}&{name}&{type}&{price}\n".format(id=self.id,name=self.name,type=self.type,price=self.price)
class PetManager(object):
"""
宠物管理
"""
__instance = None
__filename = 'pet_book.txt'
def __new__(cls,*args,**kwargs):
if not cls.__instance:
cls.__instance = super(PetManager,cls).__new__(cls,*args,**kwargs)
return cls.__instance
def add_pet(self,pet_id,pet_name,pet_type,pet_price):
pet = Pet(pet_id,pet_name,pet_type,pet_price)
with open(PetManager.__filename,'a') as fp:
fp.write(pet.get_line())
def list_all_pets(self):
all_pets = []
with open(PetManager.__filename,'r') as fp:
for line in fp:
pet = Pet.pet_with_line(line)
all_pets.append(pet)
return all_pets
def search_pet(self,name):
with open(PetManager.__filename,'r') as fp:
for line in fp:
pet = Pet.pet_with_line(line)
if pet.name == name:
return pet
return None
class Application(object):
"""
程序运行流程控制
"""
__instance = None
def __new__(cls,*args,**kwargs):
if not cls.__instance:
cls.__instance = super(Application,cls).__new__(cls,*args,**kwargs)
return cls.__instance
def __input_pet_info(self):
pet_id = input(u'请输入宠物编号:')
pet_name = input(u'请输入宠物名字:')
pet_type = input(u'请输入宠物类型:')
pet_price = input(u'请输入寄养价格:')
manager.add_pet(pet_id,pet_name,pet_type,pet_price)
print(u'恭喜!宠物添加成功!')
def __print_all_pets(self):
all_pets = manager.list_all_pets()
print('id\tname\ttype\tprice')
for pet in all_pets:
print("{id}\t{name}\t{type}\t{price}".format(id=pet.id,name=pet.name,type=pet.type,price=pet.price))
def __search_pet(self, name):
pet = manager.search_pet(name)
print(pet.id + '\t' + pet.name + '\t' + pet.type + 't' + pet.price)
def run(self):
print(u"宠物寄养系统")
print(u'0. 退出程序')
print(u"1. 添加新宠物")
print(u"2. 显示所有宠物")
print('3.查找宠物')
while True:
option = input(u'请输入操作序号:')
if option == '0':
break
elif option == '1':
self.__input_pet_info()
elif option == '2':
self.__print_all_pets()
elif option == '3':
k = input('请输入宠物名字')
self.__search_pet(k)
manager = PetManager()
app = Application()
def main():
app.run()
if __name__ == '__main__':
main()