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