首页 > 代码库 > 第十节:python异常处理、类
第十节:python异常处理、类
python个人笔记,纯属方便查询。
-----------------------python异常处理------------------------- try: name=[‘a‘,‘b‘,‘c‘] name[3] except IndexError: print ‘you list is out of range.........‘ 意思是让原本的错误改成自己想要的错误提示。 try: name=[‘a‘,‘b‘,‘c‘] #name[3] info_dic={} info_dic[‘alex‘] #except (IndexError,KeyError): except IndexError: print ‘you list is out of range.........‘ except KeyError: print ‘The key error...............‘ 自定义异常: class myexception(exception): pass try: raise myexception name=raw_input().strip() if name != ‘darren‘: raise myexception except myexception: print ‘no valid name specil!‘ 总是打印一个错误: try: name=raw_input() if len(name)=2: print ‘aaa.........‘ else: print ‘bbb..........‘ finally: print ‘going to shutdown........‘ ---------------------------------------python类------------------------------------------- 类的语法: class dog: def name(self): print "hello,master,my name is python!" D=dog() #将类赋给D这个变量 D.name() #引用dog类下面的name方法。 ---------------- #初始化函数:def __init__为初始化函数,函数里的第一个变量就是类的名字,等于把类Person传给self, #!/usr/bin/env python class Person: def __init__(self,name,age): print "i am being called right now" self.Name=name self.Age=age def sayhi(self): print "hi,my name is %s,my age:%s" %(self.Name,self .Age) p= Person(‘darren‘,22) p.sayhi() ################################################## class Dog: name=‘hanxin‘ def sayhi(self): print "hello,master,my name is python!" print "my name is:",self.name def saying(self,food): if food == ‘bone‘: print ‘i like it very much‘ else: print "do not give me" d = Dog() d.sayhi() d.saying(bone) #这里的类等于把dog传给self,self就是类。类下的多个函数是不能相互通信的,如果要通信要先传给类 ######################################### class Dog: name=‘hanxin‘ def sayhi(self): print "hello,master,my name is python!" print "my name is:",self.name likefood=‘bone‘ self.likefood=likefood #把函数变量变为类变量 def saying(self,food): if food == self.likefood: #调用其他函数的变量这里不能直接调用,而是需要调用类变量才可以。 print ‘i like it very much‘ else: print "do not give me" d = Dog() d.sayhi() d.saying(‘bone‘) ######################################## #类的继承: class schoolmember: def __init__(self,name,sex,national=‘china‘): self.name=name self.sex=sex self.national=national def tell(self): print "hi,my name is %s,i am from %s"%(self.name,self.national) class student(schoolmember): def __init__(self,NAME,SEX,CLASS,SCORE,NATIONAL): schoolmember.__init__(self,NAME,SEX,NATIONAL) self.CLASS=CLASS self.SCORE=SCORE def paytuition(self,amount): if amount < 6499: print "get the fuck off....." else: print ‘welcome onboard!‘ class teacher(schoolmember): def __init__(self,NAME,SEX,COURSE,SALARY,NATIONAL): schoolmember.__init__(self,NAME,SEX,NATIONAL) self.COURSE=COURSE self.SALARY=SALARY def teachering(self): print "i am teaching %s,i am makeing %s per month!"%(self.COURSE,self.SALARY) s1=student(‘wangzhendong‘,‘man‘,‘python‘,‘A+‘,‘china‘) s1.tell() s2=student(‘darren‘,‘man‘,‘linux‘,‘a+‘,‘us‘) s2.tell() t1=teacher(‘darren‘,‘man‘,‘couse‘,‘7000‘,‘hanguo‘) ##############################################################
本文出自 “小东哥” 博客,谢绝转载!
第十节:python异常处理、类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。