How to access kwargs created in an other function using python? -


i'm working on large finite element analysis code lot functions. pass variables between different functions make use of python's kwargs. opted can call functions in lot of different ways, creating great flexibility.

however i'm dealing problem. main function call function using kwargs. second function processes input , adds new kwargs. problem new kwargs created in function not accessible in first one. there pythonic way of doing this? created following code example:

# create main function def main(**kwargs):     # add key word argument     kwargs["textmain"] = "text added in main"      # call function existing kwargs , new 1     functiona(textnew = "text added in new" ,**kwargs)      # test in kwargs after subfunction called     print("\ntest kwargs after subfunction called:")      if "textmain" in kwargs:         print(kwargs["textmain"])      if "textnew" in kwargs:         print(kwargs["textnew"])      if "texta" in kwargs:         print(kwargs["texta"])  def functiona(**kwargs):      # test kwargs before adding kwargs in subfunction     print("\ntest kwargs in subfunction before adding:")      if "textmain" in kwargs:         print(kwargs["textmain"])      if "textnew" in kwargs:         print(kwargs["textnew"])      if "texta" in kwargs:         print(kwargs["texta"])      # add kwargs     kwargs["texta"] = "text added in sub a"      # , test kwargs again     print("\ntest kwargs in subfunction after adding:")     if "textmain" in kwargs:         print(kwargs["textmain"])      if "textnew" in kwargs:         print(kwargs["textnew"])      if "texta" in kwargs:         print(kwargs["texta"])  if __name__ == "__main__":     main() 

this yields following output:

#test kwargs in subfunction before adding: #text added in main #text added in new  #test kwargs in subfunction after adding: #text added in main #text added in new #text added in sub  #test kwargs after subfunction called: #text added in main 

where prefered output be:

#test kwargs in subfunction before adding: #text added in main #text added in new  #test kwargs in subfunction after adding: #text added in main #text added in new #text added in sub  #test kwargs after subfunction called: #text added in main #text added in new #text added in sub 

it of great if 1 solve problem. in advance!

when use **kwargs syntax in call expression, python expands dictionary , passes in separate arguments. same syntax in function signature tells python create new dictionary in function capture keyword arguments.

in other words, kwargs in main() not same dictionary 1 in functiona().

either pass in actual dictionary (not using **kwargs syntax) or have functiona() return new dictionary.

passing in kwargs single argument lets alter dictionary functiona() , see changes reflected in main(), share reference same dictionary:

def main(**kwargs):     functiona(kwargs, textnew="text added in new")     # ... etc ...   def functiona(mapping, **kwargs):     # mapping, same dictionary kwargs in main 

returning updated kwargs dictionary functiona() requires assign new object kwargs in main():

def main(**kwargs):     kwargs = functiona(textnew="text added in new", **kwargs)     # ... etc ...  def functiona(**kwargs):     # ...     return kwargs 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -