Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Best practice way to validate/parse **kwargs?

I am trying to learn more about Python - I am using 3.6.3 currently.

What is the best practice for argument validation/parsing when using kwargs?

Or, what is the best practice for argument validation/parsing without kwargs?

class foo:
  def __init__(self, **kwargs):
    if 'a' in kwargs:
      self.a = kwargs['a']
    else:
      self.a = 0

class bar(foo):
  def __init__(self, **kwargs):
    super().__init__()
    if 'x' in kwargs:
      self.x = kwargs['x']
    else:
      self.x = 23

# b and y are undefined, but my classes use kwargs - what is the best practice for validating / parsing dynamic arguments?
test = bar(b=1,y=24)
like image 784
johnnygear Avatar asked Oct 12 '17 23:10

johnnygear


2 Answers

You can pass a default value to get() for keys that are not in the kwargs dictionary:

def __init__(self, **kwargs):
    self.a = kwargs.get("a", 0)
    self.x = kwargs.get("x", 23)
    # etc.

Alternatively if you want any value in kwargs to be set as an instance variable of your class, you could do:

def __init__(self, **kwargs):
    for k, v in kwargs.items():
        self.__setattr__(k, v)
like image 98
Hat Avatar answered Oct 09 '22 05:10

Hat


class Foo:
   def __init__(self,a=0):
       self.a = a

class Bar(Foo):
    def __init__(self,b=21,**kwargs):
        self.b = b  # accept whatever you want for this child
        Foo.__init__(self,**kwargs) # pass remaining kwargs down the line

does exactly what your Foo class does and is much more clear

most problems with using kwargs come from the fact that its not at all self documenting ... I have no idea what arguments can and should be supplied for neither Foo or Bar where as explicitly declared arguments with default values make it very clear what options are available to pass into the functions

like image 24
Joran Beasley Avatar answered Oct 09 '22 06:10

Joran Beasley