码迷,mamicode.com
首页 > 编程语言 > 详细

python 中exception,class学习

时间:2014-11-07 20:29:45      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   java   sp   div   on   cti   bs   

python 中exception,class 学习

instroduction:

Object2 = Object1 ( like java)

if Object1 is class object , then copy by reference;

if Object1 is basic type, then copy by value

1. exception

主要结构:

try:

exception ValueError:

exception ZeroDivisionError:

exception NameError:

exception TypeError:

exception:

finally:

2. custom exception

class Error(Exception):
pass

def MyError(Error):
def __init__(self,value):
print value
self.value = value
def __str__(self):
return repr(self.value)

3. class

class Bird:
number = 0
def __init__(self,name):
self.name = name
print ‘Initializing %s‘ % self.name
def __del__(self):
print ‘del‘,self.number
def fly(self):
print ‘fly %d‘ % self.number
def add(self):
self.number = self.number + 1
def sub(self):
self.number = self.number - 1
class Sparrow(Bird):
def __init__(self,name,alias):
Bird.__init__(self,name)
self.alias = alias
print ‘alias is %s ‘ % self.alias
def fly(self):
print ‘Sparrow fly %s‘ % self.alias

description:

constructor: __init__

destructor:__del__

static data: number

4. file input/output

cPickle or pickle: store the object consistently

python 中exception,class学习

标签:des   io   ar   java   sp   div   on   cti   bs   

原文地址:http://www.cnblogs.com/purejade/p/4081998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!