Is there a way to "decorate" a block of Python code directly?

I have a bunch of code:

statement1(args)
statement2(args)
statement3(args)
statement4(args)
statement5(args)

I want to split statements into blocks and write to the log after each block. Logging is a bit more complicated: I want to record things like the running time of each block and the state of a particular data structure after the block is executed. So I created a decorator called log_blockthat handles all these details. Now my code is as follows:

@log_block()
def block1():
    statement1(args)
    statement2(args)

@log_block()
def block2()
    statement3(args)

@log_block()
def block3():
    statement4(args)
    statement5(args)

block1()
block2()
block3()

, . , , , , , , . , :

@log_block()
    statement1(args)
    statement2(args)

@log_block()
    statement3(args)

@log_block()
    statement4(args)
    statement5(args)

, . - ?

+4
1

- , . with, .

+4

Source: https://habr.com/ru/post/1648269/


All Articles