directory

 

A decorator

Simple decorator

More sophisticated decorators

Partial function

Scope of a variable

Exception handling

Error handling statements:

assertions


A decorator

Concept: a closure that takes a function as an argument and returns an alternate version of the function, essentially a function that returns a function

Simple decorator

def a() :
    print("this is hanshu\n")

def b(fun) :
    print("* * * * * * * * * * * *")
    fun()

b(a)
Copy the code

def a() :
    print("this is hanshu\n")

def b(fun) :
    def k() :
        print("* * * * * * * * * * * *")
        fun()
    return k

f = b(a)
f()
Copy the code

 

More sophisticated decorators

def a(ok) :
    print("this is %s\n" %ok)

a('10')

def b(fun) :
    def c(ok) :
        print('* * * * * * * *')
        if ok > 0 :
            ok = 0
        fun(ok)
    return c

a = b(a)
a(10)
Copy the code

If you want to use someone else’s method, but you can’t change someone else’s code, write a decorator to do so

You can also decorate the method that needs to be decorated with the @ decorating method which is equivalent to a = B (a)

Python2.4 began to support using @ conformance

def b(fun) :
    def c(ok) :
        print('* * * * * * * *')
        if ok > 0 :
            ok = 0
        fun(ok)
    return c

@b
def a(ok) :
    print("this is %s\n" %ok)

a(10)
Copy the code

However, you need to pay attention to the parameters because ok > 0 is used. All ok values must be integers, not strings

def b(fun) :
    def c(ok) :
        print('* * * * * * * *')
        if ok > 0 :
            ok = 0
        fun(ok)
    return c

@b
def a(ok) :
    print("this is %s\n" %ok)

a('10')
Copy the code

 

Partial function

Base =n Evaluates the string as n-base, and the output is still base 10

print(int('1010',base=10))
print(int('1010',base=8))
print(int('1010',base=2))
Copy the code

 

Scope of a variable

Scope: The scope in which a variable can be used

Variables in a program are not always available. Access depends on where the variable is assigned

Scope:

  • Global scope
  • Local scope
  • Built-in scope

 

Exception handling

print(3/0)
Copy the code

Requirement: Let the program continue executing without terminating when it encounters an error

Error handling statements:

try…… except….. else

Format:

try:

Statement t

Except error as e:

Statements 1

Except error as e:

Statements 2

.

else:

Statement e

Note: The else statement is optional

Function: Detects errors in try blocks so expect statements can catch and process error messages

Logic: When the program executes to try… expect

  1. If an error occurs when a try “statement t” is executed, the first error code is matched, and statement 1 is executed if a match occurs
  2. If the try “statement t” execution fails and no matching exception is found, the error will be submitted to the upper try statement. Or go to the top of the program
  3. If no error occurs when try “statement t”, execute statement E under else
try:
    print(3/0)
except ZeroDivisionError as e:
    print("The divisor is 0 \n")
Copy the code

try:
   # print(3/0)
    print(n)
except ZeroDivisionError as e:
    print("The divisor is zero.")
except NameError as e:
    print("There is no variable.")
Copy the code

Using except without matching the error type, an error is reported whenever an exception occurs in the program

try:
    print(n)
except :
    print("An exception occurred in the program.")
Copy the code

Using except carries multiple exceptions

try:
    print(5 / 0)
except (NameError,ZeroDivisionError):
    print("Program has no variable or divisor of zero.")
Copy the code

Errors are class errors, and all errors inherit from BaseException, so we catch errors of that type and catch all errors of subclasses

Multilayer call error

def fun1(num) :
    print(2/num)
def fun2(num) :
    fun1(num)
def main() :
    fun2(0)
try:
    main()
except (NameError,ZeroDivisionError,BaseException):
    print("Program has no variable or divisor of zero.")
Copy the code

try… except… finally…

Format:

try:

Statement t

Except error as e:

Statements 1

Except error as e:

Statements 2

.

finally:

Statement f

Statement t executes statement F with or without an error

 

assertions

If the assertion is correct, no assertion is displayed

def su(a,b) :
    assert(b ! =0),"B cannot be zero."
    print(a / b)

su(10.2)
su(10.0)
Copy the code