‘类’的复习
作业一:总结
1.什么是绑定到对象的方法,如何定义,如何调用,给谁用?有什么特性
直接定义的类, 如何定义: class A: def __init__(self,name): self.name=name def printer(self): ##绑定方法的定义 return 'name is %s'%self.name a1=A('tony') print(a1.printer()) #绑定方法调用 , 给对象调用 有什么特性:绑定到对象的特性,对象随时调用方法,将方法自己做为第一个参数传入
2.什么是绑定到类的方法,如何定义,如何调用,给谁用?有什么特性
classmethod, 如何定义: class People: def __init__(self,name,age): self.name=name self.age=age @classmethod def printer(cls,name): return (name) p1=People('tony',32) print(p1.name) #给对象用 print(p1.printer('abc')) #调用 #将类做为第一个参数传入,
3.什么是解除绑定的函数,如何定义,如何调用,给谁用?有什么特性
staticmethod , 只要是被staticmethod 修饰过的的方法,都是解除绑定方法 class C: #定义 def __init__(self,name,age): self.name=name self.age=age @staticmethod def printer(): print('welecome ') @staticmethod def read(): print('read function') c1=C('tony',21) #调用 print(c1.read()) #对象调用
4.什么是property,如何定义,如何使用,给谁用,什么情况下应该将一个属性定义成property,有什么好处?
property 是对象执行的状态,
如何定义:跟装饰器使用一样,在类里的的函数上使用@property
给谁用:指定的执行状态
有什么好处?:对象调用指定到执行状态
作业二:
要求一:自定义用户信息数据结构,写入文件,然后读出内容,利用eval重新获取数据结构
#!/usr/bin/env python #!-*- coding:utf-8 -*- def write_func(): with open('user.db','w') as file_write: file_write.write(str({'tony':{"password":"123",'status':False,'timeout':0},'liang':{"password":"456",'status':False,'timeout':0},})) def read_func(): with open('user.db','r') as file_read: f=file_read.read() data=eval(f) print(data['tony']['status']) print(data['liang']['status'])
要求二:定义用户类,定义属性db,执行obj.db可以拿到用户数据结构
class User: db_path='user.db' def __init__(self,username): self.username=username @property def db(self): data=open(self.db_path,'r').read() return eval(data) # @setattr() # def db(self): # raise TypeError:("cat't delete " # u=User('tony') # print(u.db['tony']['age'])
要求三:分析下述代码的执行流程
class User: #定义用户类 db_path='user.db' #将用户文件赋值给db_path def __init__(self,name): #定义初始函数 self.name=name @property #定义property特性函数,只要是查的操作就直接下面的函数 def db(self): with open(self.db_path,'r') as read_file: info=read_file.read() return eval(info) @db.setter #定义property设置函数setter ,只要是修改的操作就直接下面的函数 def db(self,value): # 需要一个传入参数做为修改的值 with open(self.db_path,'w') as write_file: #打开文件为写入模式 write_file.write(str(value)) #写入的值是字符串形式 write_file.flush() #每次都是即时写入硬盘 def login(self): #定义登录函数 data=self.db #将类内的db函数,打开文件的操作,取到用户信息的值赋值给data if data[self.name]['status']: #用户信息是字典格式,取到传入参数的登录状态,条件判断为值,则执行‘已经登录’,并返回‘真’ print('已经登录') return True if data[self.name]['timeout'] < time.time(): #条件判断,用户的超时的时间 小于时间戳:就执行以下代码,允许用户登录错误三次,如果用户输入正确,则data[self.name]['status']=True,将用户的 登录状态修改为‘真’,并将用户的‘超时’设置为0 ,self.db=data,将修改的信息赋给db函数,将修改的信息写入文件 count=0 while count < 3: passwd=input('password>>: ') if not passwd:continue if passwd == data[self.name]['password']: data[self.name]['status']=True data[self.name]['timeout']=0 self.db=data break count+=1 else: data[self.name]['timeout']=time.time()+10 #如果超过三次,超时+10,将修改的信息赋给db函数,将修改的信息写入文件 self.db=data else: print('账号已经锁定10秒') u1=User('egon') #对象实例化,将egon传入初始函数,赋值给u1 u1.login() #使用对象的login()函数,登录 u2=User('alex') #对象实例化,将egon传入初始函数,赋值给u2 u2.login() #使用对象的login()函数,登录
要求四:根据上述原理,编写退出登录方法(退出前要判断是否是登录状态),自定义property,供用户查看自己账号的锁定时间,
class User: db_path='user.db' def __init__(self,name): self.name=name @property def db(self): with open(self.db_path,'r') as locked_check: info=locked_check.read() data=eval(info) return 'locked time %s '%data[self.name]['locked_time'] #@property #def db(self): # with open(self.db_path,'r') as read_file: # info=read_file.read() # return eval(info) @db.setter def db(self,value): with open(self.db_path,'w') as write_file: write_file.write(str(value)) write_file.flush() def login(self): data=self.db if data[self.name]['status']: print('已经登录') return True if data[self.name]['timeout'] < time.time(): count=0 while count < 3: passwd=input('password>>: ') if not passwd:continue if passwd == data[self.name]['password']: data[self.name]['status']=True data[self.name]['timeout']=0 print('welecome') self.db=data break count+=1 else: data[self.name]['timeout']=time.time()+10 if data[self.name]['status']: return else: data[self.name]['locked_time']=time.ctime() print('locked time %s'%data[self.name]['locked_time']) self.db=data else: print('账号已经锁定10秒') # @property def logout(self): with open('user.db','r') as logout_check: f=logout_check.read() data=eval(f) # return 'login status "%s" '%data[self.name]['status'] if data[self.name]['status']: # return ('alread login ,logout? "y" or continue "n"') choice=input('alread login ,logout? "y" or continue "n": ').strip() if choice=='y': data[self.name]['status']=False self.db=data print('logout') # return 'logout' else: return u2=User('liang') u2.logout()