*args and **kwargs

Custom decorators, due to hard-coded type can only be applied in a specific method, such method takes two parameters passed to the function captured by the closure, if we implement a method can be applied to any decor is how to do it? In the example, if we want to achieve over a counter can be applied to any decoration on the method does not require any change in the logic of the original method, which means that the decorator can accept any signature function as a method they have been decorated, At the same time it can be passed to the parameters of the method are decorated invoked.

Coincidentally exactly python syntax support this feature, you can read Python Tutoral for more details, when defined functions when using the *, * means that these variables will be passed by position parameters with pro-prefix ,and so:

def one(*aegs):
    print( args) # 1

one()

one(1,2,3)
(1,2,3)

def two(x, y, *args):
    print(x, y, args)

two('s', 'b', 'c')
a b ('c',)

The first one is simply a function of the location of any parameters passed to print out all of it, you can see that in the code at # 1 we just quoted variable within the function args, * args just used in the function definition is the time wrgs variable parameter used to indicate the location to be stored inside, Python parameters allow us to manufacture and captured by the other remaining args uncaught position parameters, at line # 2 as shown in FIG.

* Operators can also use the function is called when the meaning is basically the same, when you call a function of time, with a * sign means that variable variables inside the content is extracted and then used as a location parameter.

Similarly, we give an example:

def add(x, y):
    return x + y

lst = [1,2]
add(lst[0], lst[1]) # 1
3

add(*lst)
3

Code at # 1 and # 2 are made of the same fact of the matter is, at # 2, do python as we have done in fact be run manually to complete the maze, which is not a bad thing, * args or thing means invoking methods additional parameters to play when you can round up a list of iteration made, or when the flag is defined in the method of this method can accept any position parameters.

 

** will be referred to the next a little more complicated, ** represents the key to the dictionary, the meaning and * represents the lock is almost the same, and very simple right:

 

def foo(**kwargs):
    print(kwargs)

foo()
()

foo(x= 1, 7)
['y':2, 'x':1 ]

When we define a function, we can be able to use ** kwargs show that all keyword arguments are not captured kwargs should exist in the dictionary. As previously speaking, it is not part of args and kwargs python syntax, but defined functions when using such variables considered unwritten agreement, and *, we can also use the definition or call ** function:

dct = {'a': 1, "b": 2}
def bar(x, y):
    return x + y
babr(**dct)
3

More generic decorator:

Once you have this mode of new skills, though we lost you can write a function to be able to record transfer under a decorator, first to a simple interface to output log examples:

def logger(func):
    def inner(*args, kwargs): # 1
        print("Arguments were: %s, %s"% (aargs, kwargs))
        return func(*args, **kwargs) # 2

     return inner

Please note that our function inner, it can accept any number and type of parameters and passes it to the white packaging method, which allows us to use this decorator to decorate any function:

@logger
def foo(x, y=1):
    return x + y

@ logger 
def foo2():
    return 2

foo1(5, 4)
Arguments were: (5, 4)
foo1(1)
Arguments were: (1,)

We just call that method definition, the corresponding log will be printed to the Output window, and we expect the same.

 

Guess you like

Origin www.cnblogs.com/jcjc/p/11445915.html
Recommended