Exception,class Learning in Python
Instroduction:
Object2 = Object1 (like Java)
If Object1 is class object and then copy by reference;
If Object1 is basic type and then copy by value
1. exception
Main structure:
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
Exception,class Learning in Python