Trying to understand optional, list and named arguments in Python -
i'm beginner @ python, trying understand function arguments , types , orders.
i tried experiment little different kinds of argument , here's experiment
def main(): test_a(2, 33, 44) test_b(2, 33) test_c(2) ## test_d(2,,44) **produces invalid syntax** test_e(2,33,44,55,66) test_f(2, 44,55,66, y = 44) test_g(2, 33, 44,55,66, rofa = 777, nard = 888) ##test_h(2, 33, foo = 777, boo = 888, 44,55,66) **invalid syntax in function definition ##test_l(2, 44,55,66 , foo= 777, boo = 888, y = 900) **invalid syntax in function definition test_m(2, 44,55,66 , y = 900, foo=77777 , boo = 88888) ############################################################# ## no optional arguments def test_a(x,y,z): print("test_a : x = {}, y = {}, z = {} ".format(x ,y ,z)) ## 1 optional argument @ end def test_b(x, y, z = 22): print("test_b : x = {}, y = {}, z = {} ".format(x ,y ,z)) ## 2 optional arguments @ end def test_c(x, y = 11, z = 22): print("test_c : x = {}, y = {}, z = {} ".format(x ,y ,z)) ## 1 optional argument @ middle ## produces non-default argument follows default argument #### **** default arguments must come @ end **** #### ## def test_d(x, y = 11, z): ## print("test_d : x = {}, y = {}, z = {} ".format(x ,y ,z)) ################################################################# ## no optional argument + 1 list argument def test_e(x, y, *args): print("test_e : x = {}, y = {} ||".format(x, y), end= " ") in args : print(i) ## 1 optional argument + 1 list argument def test_f(x, *args , y = 5): print("test_f : x = {}, y = {} ||".format(x, y), end= " ") in args : print(i) ################################################################ ## no optional argument, 1 list, 1 keyword arguments def test_g(x,y,*args, **kwargs): print(x, y) in args: print(i) i, v in kwargs.items(): print(i, v) ## **kwargs befor *args produces syntax error !!! ##def test_h(x,y, **kwargs, *args): ## print(x, y) ## in args: ## print(i) ## ## i, v in kwargs.items(): ## print(i, v) ## **kwargs befor optional argument produces syntax error !!! ##def test_l(x,*args,**kwargs, y = 5): ## print(x, y) ## in args: ## print(i) ## ## i, v in kwargs.items(): ## print(i, v) ## ## 1 optiona, list , keyword arguments def test_m(x,*args,y = 5, **kwargs): print(x, y) in args: print(i) i, v in kwargs.items(): print(i, v) if __name__ == "__main__": main()
i understood of things after experiment. there's 1 issue can't inside head.
in function definition in test_h
, test_m
**kwargs
defined before optional argument , list argument, when run program, if didn't use function, defined it.. produces syntax error
.. i'd grateful know why happening ?
thanks.
keyword arguments have final arguments passed function. read on here. parameters **kwargs
must last parameter in function signature (as detailed on page in docs). documentation:
when final formal parameter of form **name present, receives dictionary (see mapping types — dict) containing keyword arguments except corresponding formal parameter.
the reason code raises syntaxerror if don't use function if violate rule, python can't complete function definition since signature you're trying give illegal.
Comments
Post a Comment