Python basic grammar learning (4)

11 Python Exceptions

11.1 Exception capture

try: 
    # Normal code 
except (exception type): 
    # Processing when an exception is caught, multiple exception types can be caught, separated by commas # 
    If the exception type is not filled in, all exceptions will be caught 
else: 
    # Operation when there is no exception , this can be added or not, if not added, just exit the hierarchical relationship directly 
finally: 
    # It can be executed regardless of whether there is an exception
  • If an exception occurs when the statement after try is executed, python jumps back to try and executes the first except clause that matches the exception. exception).
  • If an exception occurs in the statement after try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (this will end the program and print the default error message).
  • If no exception occurs during the execution of the try clause, python will execute the statement after the else statement (if there is an else), and then control flow through the entire try statement.

11.2 raise exception thrown

        When our program has an abnormal judgment, we can actively throw an exception to interrupt the current program through raise, and throw it to the upper program for capture.

try: 
   a = 10 
   if(a < 18): 
       raise Exception('You are underage and cannot work') 
except Exception as e: 
    print(e)

11.3 Custom exceptions

        Sometimes we need a lot more judgments when designing programs, so we need to have our own exception class when we hit our own exception judgment. At this time, we can create a new exception class. New exceptions need to inherit from the Exception class, either directly or indirectly.

class myException(Exception): 
    def __init__(self,code,message): 
        self.message = message 
        self.code = code 
try: 
   a = 10 
   if(a < 18): 
       raise myException('1001','You are underage Can't work') 
except myException as e: 
    print(e.code,e.message)

12 Python modules

12.1 Definition of modules

        A file of classes, functions, and variables defined in a Python module. Some functions can be implemented separately in the module, and different functions can be realized through different modules. In the project, the module can be directly imported for use. This can help us to carry out the code Independence and decoupling.

12.2 Importing modules

[from module] import [module | class | variable | function | *] [as alias]

  1. import module # Import a certain module, after importing, you can use all the methods under the module, etc., using method: module.method
  2. form module import class|variable|function # Import a class or variable or method of a module
  3. from module import * # Import all the functions of a module, and use the method name directly when using it. The difference from the first one is that it is different when using it.

Note: There is an __all__ variable when the module is imported. If it is not set, the default __all__ variable is all the content. If it is set, only the set content will be imported when import *. The __all__ variable only works on *, and is not affected by the specified import.

12.3 Custom modules

        When writing code normally, it is inevitable to implement a self-defined module. A module in python is a python file, and the file name is actually our module name.

        Therefore, to customize the module, the first step is to create a new .py file, and the second step is to import the file name.

12.4 Python packages

        We mentioned above that a Python module is actually a Python file, so if there are too many modules, there will be a lot of files, so how to classify the files? The Python package is to classify modules. It is actually a folder, which contains a __init__.py file. This file proves that this path is a unified package.

        Importing Python packages is the same as importing modules.

Guess you like

Origin blog.csdn.net/weixin_64940494/article/details/126398924