paint-brush
The Surprising Case Of Mutable Default Argumentsby@h3avren
385 reads
385 reads

The Surprising Case Of Mutable Default Arguments

by Ajay Singh Rana3mJanuary 31st, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A Python function takes in an argument which also has a default value assigned to it. Default arguments are bind to the function as soon as the function is defined. But default arguments are mutable so we get the same results instead of a new list being created on each function call. And the same list is being mutated/modified again and again. We have a single list and the default argument always modifies the same thing. The default argument is a mutable list which is mutable and that's what is happening here.
featured image - The Surprising Case Of Mutable Default Arguments
Ajay Singh Rana HackerNoon profile picture
Ajay Singh Rana

Ajay Singh Rana

@h3avren

Dreaming with Python... Under a sky in India...

About @h3avren
LEARN MORE ABOUT @H3AVREN'S
EXPERTISE AND PLACE ON THE INTERNET.
L O A D I N G
. . . comments & more!

About Author

Ajay Singh Rana HackerNoon profile picture
Ajay Singh Rana@h3avren
Dreaming with Python... Under a sky in India...

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite