Object Oriented Primary Chapter
Object-oriented Advanced Chapter
Object-oriented other related content
One, isinstance (obj, CLS)
Checks if obj is an object of class CLS
The following is an explanation of the pseudo-code form
| 123456 |
classFoo(object): pass obj = Foo()isinstance(obj, Foo) |
The following is an explanation of the use of historical knowledge
1 ## #命令行解释2>>> a = [a] 3>>> type (a)##传统的判断方法4<class 'List'>5>>>ifType (a) isListPrint(a)## #传统的程序实现的判断方法6 ...7[1, 2, 3]8>>> isinstance (a,list)## #实例a is an object of the class list9 TrueTen>>> isinstance (a,dict)## #实例a is not an object of class Dict OneFalse
python
Second, Issubclass (sub, super)
Check if the sub class is a derived class of super class
1 class Foo (object): 2 Pass 3 4 class Bar (Foo): 5 Pass 6 7 Issubclass (Bar, Foo)
issubclass pseudo code form explanation
Three-Exception handling
Basic implementation Code
1 #! /usr/bin/env python2 whileTrue:3NUM1 = input ('NUM1:')4num2 = input ('num2:')5 Try:6NUM1 =Int (NUM1)7num2 =Int (num2)8result = Num1 +num29 #except exception,e: # #2.7 notationTen exceptValueError as E: One Print("Value Err", E) A - exceptException as E: - Print('An exception occurred with the following information:') the Print(e) - - " " - the exception type can be similar to the IF elif else notation. Get multiple known types, make it easy to debug yourself, exception implement backstop. But there are some grammatical errors, or can not be crawled, this is due to the program during the loading process has occurred, has not yet reached the program processing stage + " "exception handling basic implementation code
2 Exception types
Common exceptions
Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo does not have a property xioerror input / output exception; it is basically impossible to open the file Importerror the module or package cannot be introduced is basically a path problem or name error Indentationerror syntax error (subclass); The code is not aligned correctly indexerror the subscript index exceeds the sequence boundary, for example, when X has only three elements, it attempts to access x[5]keyerror Attempting to access a key that does not exist in the dictionary keyboardinterrupt Ctrl+c is pressed nameerror use a variable that has not been assigned to the object SyntaxError Python code is illegal, the code cannot compile (personally think this is a syntax error, Wrong) TypeError the incoming object type is not compliant with the requirements Unboundlocalerror attempts to access a local variable that is not yet set, basically because another global variable with the same name causes you to think that you are accessing it valueerror a value that the caller does not expect , even if the type of the value is correct
Common Exceptions
Object-oriented--python