Class8-recognition of object-oriented and class8-recognition of object-oriented

Source: Internet
Author: User

Class8-recognition of object-oriented and class8-recognition of object-oriented
I. isinstance (obj, cls)

Check whether obj is a cls object and return a Boolean value.

class Foo(object):    passobj = Foo()ret = isinstance(obj,Foo)
Print ret


II,Issubclass (sub, super)

Check whether the sub class is a super derived class.

class Foo(object):    passclass Bar(Foo):    passret = issubclass(Bar, Foo)
Print ret


Iii. Exception Handling

Common exceptions

AttributeError attempts to access a tree without an object, such as foo. x, but foo has no attribute xIOError input/output exception. Basically, the file ImportError cannot be opened and modules or packages cannot be introduced; basically, it is a path problem or name error. IndentationError syntax error (subclass). The Code does not correctly align the IndexError subscript index beyond the sequence boundary. For example, if x has only three elements, however, I tried to access x [5] KeyError. I tried to access the KeyboardInterrupt Ctrl + C key that does not exist in the dictionary. I was pressed NameError and used the SyntaxError Python code, which is invalid, code cannot be compiled (I personally think this is a syntax error and I wrote an error) TypeError passed in the object type and the required non-conforming UnboundLocalError attempts to access a local variable that has not been set yet, basically, because there is another global variable with the same name, you think that you are accessing its ValueError and passing in a value that the caller does not expect, even if the value type is correct

More exceptions


Universal Exception Handling Module


Complete exception Structure

Try: # main code block passexcept KeyError, e: # When an exception occurs, execute the block passelse: # after the main code block is executed, execute the block passfinally: # whether the exception or not, and finally execute the block pass

Actively trigger an exception: raise

Try: raise Exception ('error... ') Failed t Exception, e: print e

Custom exception

Exception KeyEerror Description: It is learned from the source code that the base class of KeyError is Exception, and Exception is the base class of all exceptions, so we can customize the Exception and let it inherit the Exception class, for example:

Class MyException (Exception): def _ init _ (self, msg): self. message = msg def _ str _ (self): return self. messagetry: raise MyException ('My exception') failed t MyException, e: print e


Assert Condition

In Python, assert is used to determine whether the statement is true or false. If it is false, the AssertionError is triggered.

A = 23a-= 1 assert a = 23 # When the condition is False, the AssertionError is returned ########### Traceback (most recent call last): File "D:/py-project/test/", line 5, in <module> assert a = 23 AssertionError #############

Iv. Reflection

The reflection function in python is provided by the following four built-in functions: hasattr, getattr, setattr, delattr

Modify the four functions for internal execution of the object: hasattr check whether a member getattr is contained to obtain the member setattr. Set the member delattr to delete the member class Foo (object): def _ init _ (self): self. name = 'wupeiqi 'def func (self): return 'func' obj = Foo () #### check whether a member is contained #### hasattr (obj, 'name') hasattr (obj, 'func') #### obtain a member ### getattr (obj, 'name') getattr (obj, 'func ') ##### set members #### setattr (obj, 'age', 18) setattr (obj, 'show ', lambda num: num + 1) ##### delete a member #### delattr (obj, 'name') delattr (obj, 'func ')

Related Article

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: 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.