Day13 modules and exceptions

Day13 modules and exceptions

1. Generator

1. What is a generator

1) Container (a container that can create multiple data), the method of creating data is saved in the generator, not the data itself.
2) Features:
a. The print builder cannot see the element;
b. The number of elements cannot be counted by len;
c. If you need the data in the generator, you must take out the data, and if you take one, there will be one less

3) The way the generator gets the elements is exactly the same as the way the iterator gets the elements

2. How to create a generator

You can get a generator by calling a function with the yield keyword.

def func1():
    print('hello')
    yield
    return 100


result = func1()
print(f'result:{result}')

3. How to control the number and value of elements in the generator

When executing the function body of a common generator function, you will encounter yield several times, and the corresponding generator can create several data. Every time you encounter yield, the value after yield is the data that can be created.

def func2():
    print('+++++')
    yield 100
    print('------')
    yield 200
    print('=======')
    yield 300

gen2 = func2()
print(gen2)
print('next:', next(gen2))
print('next:', next(gen2))
print('next:', next(gen2))
# print('next:', next(gen2))        # Report an error!
print(list(gen2))       # []
def func3(n):
    yield 100
    if n % 2 == 0:
        yield 200
    yield 300


gen3 = func3(3)
print(gen3)
print(list(gen3))       # [100, 300]
# print(next(gen3))     # Report an error!

Two, the module

1. What is a module

A py file in python is a module.

2. How to use content from another module in one module

1) Prerequisite: If you want to use the content in another module in one module, the module name of the used module must meet the requirements of variable names

2) import module
a. import module name - import the specified module, after importing, you can use all the contents of this module in the way of 'module name.xxx'
b. from module name import content 1, content 2, … - Import the specified module, and the specified content can be used directly after importing
c. from module name import * - Import the specified module, after importing, you can directly use all the content in the module
d. import module name as new module name - Rename the corresponding module after importing the specified module: 'new module name.xxx'
e. from module name import content 1 as new content 1 - rename the imported content

# --------------- a. Import method 1 ----------------
import test

print(test.a)
print(test.name)
test.f_test1()
# --------------- b. Import method 2 ----------------
from test import a, f_test1

print(a)
f_test1()
# print(name)       # Report an error!
# --------------- c. Import method 3 ----------------
from test import *

print(a)
print(name)
f_test1()
# --------------- d. Import method 4 ----------------
import test as t1

test = 1000
print(t1.a)
print(t1.name)
t1.f_test1()
print(test)
--------------- e.Import method 5 ----------------
from test import name as t_name, a

name = 'little flower'

print(name)
print(t_name)
print(a)

Three, package

1. What is a package

Folders containing __init__.py files are packages.

2. How to use the content of the module in the package or folder

1) import package name - (only applicable to packages) import the specified package, after importing, you can use all the contents in the __init__.py file in the package through 'package name.xxx'
2) from package name import module name 1, module name 2,... - Import the specified module in the specified package, after importing, you can use the content in the specified module through 'module name.xxx'
3) from package name import content 1, content 2,... (only applicable to packages) import the specified content in the __init__.py file in the specified package
4) import package name. module name
5) from package name. module name import content 1, content 2,...

# -----------------Import method 1-----------------
import files2

files2.f_test2()
print(files2.pi)
# -----------------Import method 2-----------------
from files2 import test2, test4, pi, f_test2

print(test2.msg)
print(test4.abc)
print(pi)
f_test2()

from files import test1
print(test1.money)

from files2.abc import test3
print(test3.demo)
# -----------------Import method 3-----------------
import files2.test2
print(files2.test2.msg)

import files2.test2 as t2
print(t2.msg)

import files2.abc.test3 as t3
print(t3.demo)
# -----------------Import method 4-----------------
from files2.test2 import msg
print(msg)

from files2.abc.test3 import demo
print(demo)

4. Exception capture

1. Exception - an error in the program

An exception (error) in the program will directly end the program (where the exception occurs, the program will end there)

2. Exception capture - so that the program does not end when an exception occurs, and can be executed later

1) Grammatical structure 1 - catch all exceptions

try:
    code snippet 1(Code that needs to catch exceptions)
except:
    code snippet 2(Code that will be executed after an exception occurs)
finally:
    code snippet 3(Finally the code that will be executed anyway)

Execution process: Execute code segment 1 first. If there is no exception when executing code segment 1, code segment 2 will not be executed; if an exception occurs, the program will not report an error and immediately execute code segment 2

try:
    age = int(input('Please enter age:'))
    print(age)
except:
    print('The age input is wrong!')

2) Grammatical structure 2 (recommended!) - Catch exceptions of specified types

try:
    code snippet 1(Code that needs to catch exceptions)
except exception type:
    code snippet 2(Code that will be executed after an exception occurs)
finally:
    code snippet 3(Finally the code that will be executed anyway)

3) Grammar Structure 3 - Simultaneously capture multiple

try:
    code snippet 1(Code that needs to catch exceptions)
except (exception type 1, exception type 2, ...):
    code snippet 2(Code that will be executed after an exception occurs)
finally:
    code snippet 3

4) Grammar Structure 4 - Simultaneously capture multiple

try:
    code snippet 1
except exception type 1:
    Code snippet 11
except (exception type 2, exception type 22, ...):
    Code snippet 22
except exception type 3:
    Code snippet 33
...
finally:
    code snippet 3
try:
    print('abc'[4])
except KeyError:
    print('error!')
finally:
    print('suicide note')

Tags: Python

Posted by Teach on Thu, 23 Mar 2023 11:57:43 +0530