Python3 variable parameters to achieve comment +

I. Description

About 1.1 Notes

Notes about this thing, the first thing is certain methods often see top @override like in school when the University of java, on the one hand but on the other hand does not know its role seems to have no influence removed, so they do not care how .

This year to see the development of the code also see a lot of notes, regardless of their use are basically similar and online "In order to open the XXX features we need to add @XXX annotated" / "Add @XXX comment function to turn on XXX," I do not know its principles feeling quite uncomfortable so we studied doubled.

 

About 1.2 variable parameters

The so-called variable parameters, the most important refers to the number of arguments passed to the called function is uncertain.

The variable parameter should is very common, such as the standard C main function would be written as int main (int argc, ** char argv), another example of a very common print () function is the most typical variable parameter function.

But one in a very long period of time and can not understand the main functions and common functions in fact no difference, on the other hand that print () function to achieve a very complex system, so they all did not understand how to implement variable parameters should be passed.

 

About 1.3 annotations and variable parameters have anything to do

Annotations and variable parameters, in the sense that no relationship. But when I went Implementation Notes, find the need to solve the problem to make the variable parameter annotations can act on the function of the number of different parameters.

And speaking notes should act on the function of the number of different parameters is a popular demand, the notes and the variable parameters associated with the relationship still great.

 

Second, the comment code implementation

2.1 annotated function without parameters

# Function for performing a modification 
# key a: outer function has only one parameter, which is modified for receiving function itself 
DEF decorate_function (need_decorate_function_name):
     DEF decorated_function ():
         # Key II: modified function call before / after doing something else 
        Print ( " Calc Staring ... " )
         # key point three: intact function calls are modified 
        need_decorate_function_name ()
         Print ( " Calc Finished ... " )
     # key point four: in the last after the modification of the function return back to 
    return decorated_function 

# a simple function of seeking unity 
@decorate_function
 DEF calc_sum (): 
    a= 1
    b = 2
    sum_value = a + b
    print(f"{a} + {b} = {sum_value}")

if __name__ == "__main__":
    calc_sum()

The final execution results are as follows:

 

2.2 annotated function has parameters

# Function for modification of 
DEF decorate_function (need_decorate_function_name):
     # key one: the inner function has been modified to use and function exactly the same parameters to undertake to 
    # of course not the same as a parameter name had not really matter, but in order to save all as can 
    DEF decorated_function (a, B):
         Print ( " Calc Staring ... " )
         # key two: the inner function of the received parameters are passed unchanged again modified to function 
        need_decorate_function_name (a , B)
         Print ( " Calc Finished ... " )
     return decorated_function 

# a simple function of seeking engagement 
@decorate_function
 DEF calc_sum (a, B): 
    sum_value = a + b
    print(f"{a} + {b} = {sum_value}")

if __name__ == "__main__":
    calc_sum(1, 2)

The final execution results are as follows:

 

2.3 annotated function returning

# A modification function for 
DEF decorate_function (need_decorate_function_name):
     DEF decorated_function (A, B):
         Print ( " Calc Staring ... " )
         # Key a: receiving a good return value of the function is modified 
        Result = need_decorate_function_name (A , B)
         Print ( " Calc Finished ... " )
         # key two points: the end of the return value of the function to be modified is returned unchanged to the upper 
        return Result
     return decorated_function 

# a simple function of seeking engagement 
@decorate_function
 DEF calc_sum ( A, B): 
    sum_value = A + b
    return sum_value

if __name__ == "__main__":
    a = 1
    b = 2
    sum_value = calc_sum(a, b)
    print(f"{a} + {b} = {sum_value}")

Execution results are as follows:

 

2.4 annotated with a plurality of functions and the number of parameters thereof inconsistent

# A function for modification 
DEF decorate_function (need_decorate_function_name):
     # key one: use * args, ** kwargs undertake all parameters 
    DEF decorated_function (args *, ** kwargs):
         Print ( " Calc Staring ... " )
         # key point II: exactly the same directly to * args, ** kwargs function can be modified to pass 
        the Result = need_decorate_function_name (args *, ** kwargs)
         Print ( " Calc Finished ... " )
         return the Result
     return decorated_function 

# a simple combined function evaluates 
@decorate_function
 DEF calc_sum_2(a, b):
    sum_value = a + b
    return sum_value

# 一个简单的求合函数
@decorate_function
def calc_sum_3(a, b, c):
    sum_value = a + b + c
    return sum_value

if __name__ == "__main__":
    a = 1
    b = 2
    c = 3
    sum_value_2 = calc_sum_2(a, b)
    print(f"{a} + {b} = {sum_value_2}")
    sum_value_3 = calc_sum_3(a, b, c)
    print(f"{a} + {b} + {c} = {sum_value_3}")

Execution results are as follows:

 

2.5 Use annotations within the class

class : the Test
     # function for performing a modification 
    # Key a: identity firmly, the outer function and only a parameter which is modified for receiving function 
    DEF decorate_function (need_decorate_function_name):
         # Key II: firmly agree * args, ** kwargs can undertake all parameters, including the self, including 
        DEF decorated_function (args *, ** kwargs):
             Print ( " Calc Staring ... " )
             # key point three: unswervingly agree * args, ** kwargs can be modified all the parameters passed to the function, including self, including 
            the Result = need_decorate_function_name (args *, ** kwargs)
             Print ( " Calc Finished ... " )
             return the Result
        return decorated_function 

    # a simple function of seeking engagement 
    @decorate_function
     DEF calc_sum_2 (Self, A, B): 
        sum_value = A + B
         return sum_value 

    # a simple function of seeking engagement 
    @decorate_function
     DEF calc_sum_3 (Self, A, B, C): 
        sum_value + A + B = C
         return sum_value 

IF  the __name__ == " __main__ " : 
    obj = the Test () 
    A =. 1 
    B = 2 
    C =. 3 
    sum_value_2= obj.calc_sum_2(a, b)
    print(f"{a} + {b} = {sum_value_2}")
    sum_value_3 = obj.calc_sum_3(a, b, c)
    print(f"{a} + {b} + {c} = {sum_value_3}")

Execution results are as follows:

 

Third, the nature of variable parameters to achieve

When python function call, passing parameters in two ways, one is passed in the form of a position (e.g., test (a)), a form is "k = v" is passed (eg test (a = 1 )). The same "k = v" form must be located after the parameters.

(Further, the Python parameters to define a function which is similar in two forms, one is no default parameter values ​​(position parameter, such as def test (a)), one is a default value of the parameter (default parameters, DEF test after (a = 1)). in addition the default parameters must be in the position parameter. but first, we do not need to worry about here parameter passing formal parameters of function definition)

Demo program are as follows:

# 一个简单的求合函数
def calc_sum(a, b, c, d, e):
    sum_value = a + b + c + d + e
    return sum_value

# 此函数只单纯调用calc_sum()
def call_calc_sum(a,*args,**kwargs):
    sum_value = calc_sum(a,*args,**kwargs)
    return sum_value

call_calc_sum(1, 2, 3, e=4, d=5)

 

3.1 从参数变为*args, **kwargs的过程

被调用函数通过以下步骤提取参数:

第一步,如果前面有非*args, **kwargs的参数,则在传来的参数中先分配给他。比如这里在*args前面有a,所以就把第一个参数值1赋给a。

第二步,将其他非k=v形式的参数,组成元组赋值为args。比如这是把下来的2,3组成(2,3)。

第三步,将其他的k=v形式的参数,组成字典赋值给kwargs。比如这里把e=4,d=4组成['e': 4, 'd': 5]。

 

 

3.2 从*args, **kwargs变回具体参数的过程

被调用函数通过以下步骤提取参数:

第一步,如果开头有非*args, **kwargs的参数,则将按正常参数解析。如1赋给第一个参数a。

第二步,将元组参数按顺序分发给接下来的参数。如将2赋给下来的第二个参数b,再将3赋给下来的第三个参数c。

第三步,将字典参数,按设置的k/v分发给对应的参数。如按e=4赋给第五个参数e,按d=5赋值给第四个参数d。

 

Guess you like

Origin www.cnblogs.com/lsdb/p/12018942.html