Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deal with undefined arguments more elegantly

Tags:

python

The accepted paradigm to deal with mutable default arguments is:

def func(self, a = None):
    if a is None:
        a = <some_initialisation>
    self.a = a

As I might have to do this for several arguments, I would need to write very similar 3 lines over and over again. I find this un-pythonically a lot of text to read for a very very standard thing to do when initialising class objects or functions.

Isn't there an elegant one-liner to replace those 3 lines dealing with the potentially undefined argument and the standard required copying to the class instance variables?

like image 219
K.-Michael Aye Avatar asked Mar 25 '12 18:03

K.-Michael Aye


People also ask

Can I assign undefined to a variable?

YES, you can, because undefined is defined as undefined.

How do you make a value undefined?

You will get undefined value when you call a non-existent property or method of an object. In the above example, a function Sum does not return any result but still we try to assign its resulted value to a variable. So in this case, result will be undefined.

What is def in Python with example?

In Python, defining the function works as follows. def is the keyword for defining a function. The function name is followed by parameter(s) in (). The colon : signals the start of the function body, which is marked by indentation. Inside the function body, the return statement determines the value to be returned.

How do you define a parameter in Python?

Here are simple rules to define a function in Python. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.


2 Answers

If a "falsy" value (0, empty string, list, dict, etc.) is not a valid value for a, then you can cut down the initialization to one line:

a = a or <initialize_object>
like image 122
kristaps Avatar answered Oct 16 '22 23:10

kristaps


Another way of doing the same thing is as follows:

def func(self,**kwargs):
    self.a=kwargs.get('a',<a_initialization>)
    ...

This has the added bonus that the value of a passed to the function could be None and the initialization won't overwrite it. The disadvantage is that a user using the builtin help function won't be able to tell what keywords your function is looking for unless you spell it out explicitly in the docstring.

EDIT

One other comment. The user could call the above function with keywords which are not pulled out of the kwargs dictionary. In some cases, this is good (if you want to pass the keywords to another function for instance). In other cases, this is not what you want. If you want to raise an error if the user provides an unknown keyword, you can do the following:

def func(self,**kwargs):
    self.a=kwargs.pop('a',"Default_a")
    self.b=kwargs.pop('b',"Default_b")
    if(kwargs):  
        raise  ... #some appropriate exception...possibly using kwargs.keys() to say which keywords were not appropriate for this function.
like image 32
mgilson Avatar answered Oct 16 '22 23:10

mgilson