3

I have a dictionary (arguments_fun) with arguments I want to pass to a function (fun). In Example1 the code works:

class Example1: 
  arguments_fun={'a': 2, 'b': 3, 'c': 4} #substitute this into function arguments
  def fun (self, **kwargs):
     print(kwargs['a'], kwargs['b'], kwargs['c'])
a=Example1()
a.fun(**a.arguments_fun)

Out: 2 3 4 

However I have some extra requirements:

  • I want fun to take arguments_fun directly as default.
  • I want to be able to pass it x arguments which override the argument_fun values.

here is an example code:

class Example2: 
  arguments_fun={'a': 2, 'b': 3, 'c': 4} #substitute this into function arguments
  def fun (self, **arguments_fun):  #Pass dictionary directly to fun
    print(arguments_fun['a'], arguments_fun['b'], arguments_fun['c'])
a=Example2()
a.fun()
a.fun(b=7) #b=7 overrides the arguments_fun value

desired Output: 
Out: 2 3 4
Out: 2 7 4 

Basically Example2 should work as Example3:

class Example3: #Example 2 shuld work as if it was like this: 
  def fun (self,a=2, b=3,c=5 ):  #Pass dictioary directly to fun
     print(a,b, c)

a=Example3()
a.fun()
a.fun(b=7) 

Out: 2 3 4
Out: 2 7 4    

The purpose is so that I can write other functions that share the same arguments.

Leo
  • 1,176
  • 1
  • 13
  • 33

0 Answers0