Modern programming languages, can not be less than object-oriented and abnormal capture, but Python has a lot of own things (of course, with Java, C ). Python has its own garbage collection mechanism, with no effort to prevent memory from leaking, but resource management is still to be worried.
Fourth, object-oriented 1, class with object class myclass: pass
Method (ie, member function: Each function has at least one parameter Self, pointing to instances of this class) Initialization function: (equivalent to constructor) DEF __INIT __ (Self, Argument, ...): # Unlike constructor, object construct The first automatic call. #Self is equivalent to the THIS pointer in C , but explicitly defined, the later is the initialization parameter destructor (when the object is 0 is 0, automatically call) DEF __DEL __ (Self): Unique function: (actually Extended type syntax allows users to customize semantics. Many still not listed) __repr __ (self) __str __ (self)) Note Returns Returning in Print Obj__cmp __ (self)) # Implement the comparison operator, make the object Available> Access: aclass.count # aclass a a.count # is different from aclass.count a .__ class __. Count # with variables defined in aclass.count object variable __init __ (), as of the following x, y classes point: def __init__ (Self, x = 0, y = 0): Self.x = x self.y = Y access method P = P = POINT (1, 2) PX package class method and attribute is public, or private, can be from its name judgment. If the name begins with two underscores, but not ending with two underscores, it is private. The rest are public. Some properties of the class __name__ attribute return the name of the class, is a string type. __Doc__ attribute is a document that returns a description class. __Dict__ attribute is a dictionary that includes attributes of classes. __Module__ The property is used to determine which module belongs to. Python supports cross-mode inheritance. __Bases__ The parent class sequence of the attribute reverse subclass. Some properties of the object __class__ indicate which class is initialized from which the instance is initialized. __Dict__ use a dictionary storage instance's properties. Some built-in methods for classes in Python: IssubClass (C2, C1) # Tests if the previous class is a subclass of the latter class. Its two parameters must be class. IsInstance (OBJ1, C1) # tests if an object is an instance of a given class, and its second parameter must be class. 2, inheritance and polymorphism (support Multi-inheritance) Class DerivedClass (BaseClass1, BaseClass2, ...): Pass (method and attribute can inherit, but the __doc__ attribute of the subclass cannot inherit the parent class) function overload (overload) The same name Method, the subclass covers the parent class. Method of calling the parent class Baseclass1.Method (Self, arguments) method with the inheritance permission of the attribute is not protected, only private, private subclass initialization __init __ () first explicitly adjusts the parent class BaseClass1 .__ init__ (Self , arguments) Abstract class Class AbstractClass: Def AbstrctMethord (self): Raise NotimplementError # Critical Here 5, exceptions in C can initiate stack deployment, layers release memory until the exception is captured to eliminate memory leaks. Python is different because of garbage collection, may be different, but it is quite right. Abnormal processing: method 1 (Try-Except form): Try: ... # Exception code may occur. Except ExceptionType1, ExceptionType2: ... # Specific 1, 2 Except ExceptionType3, ExceptionType4: ... # Specific 3, 4 Except: ... # All unknown exceptions ELSE : ... # 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无 无Finally: ... # Regardless of whether there is an exception to execute the statement to be executed, it is generally used to release the resource exception: Raise UserList (["" Error NO: 1 "," Catcher: set "]) Raise MyError," Hello World " # Throw an exception class, and identify an abnormal object parameter except userlist, arg: # This exception capture, userlisat is a handable anomaly class, ARG processed object, here is a List object for K IN arg: Print K The exception reception parameter format is as follows: Except Exception, argument: exception_handling except (Exception1, Exception2, ..., Exceptionn, argument: exception_handling