0

What i want is the arguments to if not set, call a function to fetch their values. What i've used now is

class Testing():
      object2 = test2()
      def test(self, **kwargs)
         if not kwargs.get('a'):
            a= self.object2.getvalue('a')
         else:
            a= kwargs.get('a')

         if not kwargs.get('b'):
            b= self.object2.getvalue('b')
         else:
            b= kwargs.get('b')
         print a, b

What i'd like is something like this:

class Testing():
      object2 = test2()
      def test(a = self.object2.getvalue('a'), b= self.object2.getvalue('b'), **kwargs)
         print a, b

Any suggestions?

Sillyreduction
  • 147
  • 1
  • 2
  • 10
  • You can't do that, stick with what you currently have. Default values are evaluated when the method is *defined*, not when it's *called* (and `self` isn't in scope at that time anyway). – jonrsharpe Jul 13 '16 at 13:08
  • 1
    What's wrong with `a = kwargs.get('a', self.object2.getvalue('a'))` ? – Burhan Khalid Jul 13 '16 at 13:10
  • Does `a = kwargs.get('a', self.object2.getvalue('a'))` try first argument and then proceeds to the second? Haven't used **kwargs tried finding some info and what you see in the main question is what i found. Couldn't find it's documentation – Sillyreduction Jul 13 '16 at 13:17
  • `kwargs` doesn't really have documentation, it's just the conventional name given to a dictionary of arbitrary keyword arguments. `d.get(key, default)` will try to find `key` in `d`, return the value (`d[key]`) if it exists and `default` otherwise. – jonrsharpe Jul 13 '16 at 15:15

0 Answers0