1

Here's an example of Python code:

def test(a=[]):
  a.append('a')
  return a

test()
test()
test()
test()

print(test()) # prints ['a', 'a', 'a', 'a', 'a']

Why is Python calculating the argument a before any function calls and not with each function call, as in, for example, JavaScript? I find this behaviour quite strange. Is it a bug or intentional design?

Here's an example in JavaScript

function test (a=[]) {
  a.push('a')
  return a
}

test()
test()
test()
test()

console.log(test()) // prints ['a']
Vlad Furman
  • 135
  • 6

0 Answers0