Python 3.x Learning Note 12 (reflection and exception)

Source: Internet
Author: User

1. Reflection
Mapping or modifying the state, properties, methods of a program while it is running through a string

GetAttr (OBJ,NAME_STR): Gets the memory address of the corresponding method in the Obj object according to the string name_str

Hasttr (OBJ,NAME_STR): A way to determine if an object in obj has a corresponding string

SetAttr (obj, ' y ', z): The SetAttr function corresponds to the function Getatt (), which is used to set the property value, which must exist.

Delattr (obj, ' y '): Removes the named property from the given object

classDog (object):def __init__(self,name): (self):Print('%s is eating ....' d= Dog ('Xiaohei')#instantiating an objectChoice = input ('>>>>:'). Strip ()#strip () remove left and right spacesifHasattr (D,choice):#Check that instantiation D has no choiceFunc = GetAttr (D,choice)#GetAttr Returns the memory address of the choice in DFunc ()#Execute function

2. Exception Basics

In the programming process in order to increase the friendliness of the program in the event of a bug will not be displayed to the user error message, but the reality of a hint of the page, popular is not to let users see the big Yellow Pages.

1) Basic structure

Try : Pass except Exception as E: Pass

2) Other structures

 #   exception structure    Try  :  #   Main code block  pass  except   Keyerror as E:  #   When an exception is executed, the block  pass  else  :  #      The main code block executes, that is, no exception, executes the block  pass  finally  :  #      pass  

3) proactively triggering exceptions

Try :     Raise Exception (' error ... ' )except  Exception as E:    print(e)

3. Common types of anomalies
Attributeerror #试图访问一个对象没有的树形, such as foo.x, but Foo has no attribute x
IOError #输入/Output exception: Basically, the file cannot be opened
Importerror # cannot introduce modules or packages; it's basically a path problem or a name error
Indentationerror #语法错误 (subclass); The code is not aligned correctly
Indexerror #下标索引超出序列边界, such as when X has only three elements, but tries to access X[5]
Keyerror # Trying to access keys that don't exist in the dictionary
Keyboardinterrupt # CTRL + C is pressed
Nameerror # using a variable that has not been assigned to an object
SyntaxError # python code is illegal, code cannot compile (personally think this is a syntax error, write wrong)
TypeError # Incoming object type does not conform to the requirements
Unboundlocalerror # attempts to access a local variable that has not yet been set, basically because another global variable with the same name causes you to think that you are accessing it
ValueError # Passes in a value that is not expected by the caller, even if the value is of the correct type

Python 3.x Learning Note 12 (reflection and exception)

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.