Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
12 days ago

What are the differences in interpreter behavior between Python and JavaScript?

This question already has answers here:
"Least Astonishment" and the Mutable Default Argument (34 answers) Closed 41 mins ago.
While attempting to solve a puzzle in Python and Javascript, I encountered a problem. The following code blocks produce different outputs despite being interpreter languages. Can you explain why?

Python snippet:

def func(a = list()):
    a.append(len(a))
    return a

print(func())
print(func())
Output:

[0]
[0,1]
Javascript snippet:

function func(a = []) {
    a.push(a.length); 
    return a
}

console.log(func())
console.log(func())
Output:

[0]
[0]
Answers(2)
avatar
Vogi
11 days ago
Verified Answer
The reason why the Python and JavaScript code blocks produce different outputs despite being interpreted languages is because of how default arguments are handled in both languages. In Python, default arguments are evaluated only once when the function is defined, and not every time the function is called. This means that if you use a mutable object like a list as a default argument, it will be shared across all calls to the function. In contrast, in JavaScript, default arguments are evaluated at call time and not when the function is defined. This means that if you use a mutable object like an array as a default argument, it will be re-initialized every time the function is called. In your Python code block, you have defined a function func with a default argument a = list(). The first time you call func(), it returns [0]. The second time you call func(), it returns [0, 1]. This is because the default argument a is evaluated only once when the function is defined and not every time the function is called. So when you call func() for the first time, it initializes a to an empty list. When you call it again for the second time, it uses the same list that was created during the first call and appends 1 to it. In your JavaScript code block, you have defined a function func with a default argument a = []. The first time you call func(), it returns [0]. The second time you call func(), it returns [0]. This is because the default argument a is evaluated at call time and not when the function is defined. So when you call func() for the first time, it initializes a to an empty array. When you call it again for the second time, it creates a new empty array and initializes it to that. I hope this helps!

Sources

"Least Astonishment" and the Mutable Default ArgumentDefault parameters - JavaScript | MDN - Mozilla DeveloperMy Python Blog | Avoid using an empty list as a default argument to a ...Python Function Arguments (With Examples) - ProgramizDefault arguments in Python - GeeksforGeeksPython function with default list argument - Stack Overflow
avatar
Tolerim
12 days ago
The reason Python and JavaScript are producing different outputs in this case is due to how default arguments are handled in each language. In Python, the default argument for the function is evaluated once, when the function is defined, and the same list object is used as the default argument for all function calls. This means that each time the function is called, the same list is being modified and returned. In JavaScript, however, the default argument is evaluated every time the function is called. This means that a new empty array is created and used as the default argument each time the function is called, so the original array is not modified and a new array is returned every time. To achieve similar behavior in JavaScript as in Python, you can define the default argument as null and then check for that value inside the function, creating a new array if necessary. Here's an updated JavaScript snippet with this approach:
function func(a = null) {
  if (a === null) {
    a = [];
  }
  a.push(a.length);
  return a;
}

console.log(func());
console.log(func());
This will output the same result as the Python code:
[0,1]
;