Python in Parameter *args, **kwargs

Source: Internet
Author: User

Python in Parameter *args, **kwargs

def foo (*args, * *Kwargs):
print 'args = ', args
print 'kwargs = ', kwargs
print '---------------------------------------'

if __name__ = = ' __main__ ':
Foo (1,2,3,4)
Foo (a=1,b=2,c=3)
Foo (1,2,3,4, a=1,b=2,c=3)
Foo (' A ', 1, None, a=1, b= ' 2 ', c=3) outputs the result as follows:

args  =  (1, 2, 3, 4)  
Kwargs  =  {} 

args  =  ()  
Kwargs  =  {' A ': 1, ' C ': 3, ' B ': 2} 

args  =  (1, 2, 3, 4)  
Kwargs  =  {' A ': 1, ' C ': 3, ' B ': 2} 

Strong>args
 =  (' A ', 1, None)  
Kwargs  =  {' A ': 1, ' C ': 3, ' B ': ' 2 '}

As you can see, these two are mutable parameters in Python . *args represents any number of nameless parameters, it is a tuple;**Kwargs represents the keyword parameter, it is a dict. And when using both the *args and * *Kwargs , theargs parameter must be listed before the * *Kwargs , like foo (a=1, b= ' 2 ', c=3, a ', 1, None,) If this is called, the syntax error "Syntaxerror:non-keyword arg after keyword ARG" will be prompted.

-------------------------------------------------------------------------------------------here to see the code for GitHub online:

DefLayerOp:
"Decorator for composable network layers."

DefLayer_decorated (Self,*args,**kwargs):
# automatically set a name if not provided.
Name=Kwargs.setdefault (' Name ',Self.get_unique_name (Op.__NAME__))
# figure out the layer inputs.
IfLenSelf.terminals)==0:
RaiseRuntimeError (' No input variables found for layer%s. '% name)
ElifLenSelf.terminals)==1:
Layer_input=self.terminals[0]
Else:
Layer_input=ListSelf.terminals)
# Perform The operation and get the output.
Layer_output=OpSelf, Layer_input,*Args,**Kwargs)
# Add to layer LUT.
self.layers[name] = layer_output
# This output was now the input for the next layer.
self.feed (layer_output)
# Return self for chained calls.
return self

return layer_decorated

Reference:
http:/ /blog.csdn.net/anhuidelinger/article/details/10011013


Go on
python @ usage
def funA(arg):    print ‘A‘    a=arg()@funAdef funB():    print ‘B‘


Here @ equivalent to Funa (Funb ())

The main motivation behind the adorner stems from the Python object-oriented programming, which is a decoration on the function call, which is only applied when a function or method is declared.
The adorner's syntax begins with @, followed by the name of the adorner Korean and optional parameters. Following the adorner, the modified function is declared, and the optional parameter of the modifier



Python in Parameter *args, **kwargs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.