Exception,class Learning in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.