-1

I've been looking up the information about why people should not use empty list as a function argument and pass none type as an argument and came up with a few questions. Here is my code:

def add_employee(emp, emp_list=None):
    if emp_list is None:
        emp_list = []
    emp_list.append(emp)
    print(emp_list)

And here is code without second argument's type specified:

def add_employee(emp, emp_list):
    emp_list.append(emp)
    return emp_list
  1. When I do not define emp_list as an empty list or none I can not utilize function's deafualt argument behavior: I can't call it like add_employee('Mark'), I had to add second variable to pass. Why is it good to have that backup default behaviour? Why couldn't I just leave it as emp_list.
  • 2
    First of all - [NEVER set mutable default arguments like list. It's evaluated once - at function definition and will yield unexpected (to you) results](https://stackoverflow.com/q/1132941/4046632). Whether you supply default value for argument is a design decision - it will depend on number of factors like your use case and preferences, etc. Second question is not quite clear to me. – buran Sep 30 '20 at 11:00

1 Answers1

0

Here is a great explanation of why you should avoid using mutable arguments in your defaults. Or at least use them sparingly: link

The general gist of it that the list will be created for the first time (in a location in memory) when you define the function. So as python reads the code and interprets it, you will end up creating that list once on the first 'read' of the function.

What this means is that you are not creating a fresh list each time you call that function, only when you define it.

To illustrate I will use the example from the link I shared above:

def some_func(default_arg=[]):
    default_arg.append("some_string")
    return default_arg
>>> some_func()
['some_string']
>>> some_func()
['some_string', 'some_string']
>>> some_func([])
['some_string']
>>> some_func()
['some_string', 'some_string', 'some_string']
  1. If I understood your question correctly, you are asking why you're better off defining the emp_list explicitly rather than having it outside the function. In my mind it boils down to encapsulation. You're essentially trying to make sure that your function doesn't change the behavior of anything outside of its scope so you're forced to pass it things directly and be explicit. In practice if you have a variable outside of the scope named emp_list, it is absolutely fine to just append to it as long as you understand what the expected behavior is.

  2. If you pass a list in the first bit of code as the emp_list, then the variable emp_list will contain your list a. The if statement will check if list a is None and since that check fails, it will skip the next line of assigning it a fresh empty list.

finnkauski
  • 169
  • 4
  • Thx for your second answer. When it comes to 1st question I meant why should I define this list argument as `emp_list = none` instead of just leaving it `emp_list`. IDK whether we understood each other in that. I know the trick about mutable arguments and fact that the list will updated. Just wanna know why not leave it just as `empty_list`. – fetus-elitus Sep 30 '20 at 13:14
  • So the questions is why have defaults values for arguments? If so then it depends on your use case. I mean if you define `def function(a, b)` you are required to pass both positional arguments, otherwise you can't do `function(a)` and let it infer the default value for `b`. There's no 'rule' when to use a default or not, it's more about you deciding whether or not you will want to call you function without passing that argument. – finnkauski Oct 02 '20 at 10:35
  • Thank u very much and have a nice day :D – fetus-elitus Oct 03 '20 at 11:46