$\require{AMScd}$ $\newcommand{\A}{\mathbb{A}}$ $\newcommand{\B}{\mathbb{B}}$ $\newcommand{\C}{\mathbb{C}}$ $\newcommand{\D}{\mathbb{D}}$ $\newcommand{\E}{\mathbb{E}}$ $\newcommand{\F}{\mathbb{F}}$ $\newcommand{\G}{\mathbb{G}}$ $\newcommand{\H}{\mathbb{H}}$ $\newcommand{\I}{\mathbb{I}}$ $\newcommand{\J}{\mathbb{J}}$ $\newcommand{\K}{\mathbb{K}}$ $\newcommand{\L}{\mathbb{L}}$ $\newcommand{\M}{\mathbb{M}}$ $\newcommand{\N}{\mathbb{N}}$ $\newcommand{\O}{\mathbb{O}}$ $\newcommand{\P}{\mathbb{P}}$ $\newcommand{\Q}{\mathbb{Q}}$ $\newcommand{\R}{\mathbb{R}}$ $\newcommand{\S}{\mathbb{S}}$ $\newcommand{\T}{\mathbb{T}}$ $\newcommand{\U}{\mathbb{U}}$ $\newcommand{\V}{\mathbb{V}}$ $\newcommand{\W}{\mathbb{W}}$ $\newcommand{\X}{\mathbb{X}}$ $\newcommand{\Y}{\mathbb{Y}}$ $\newcommand{\Z}{\mathbb{Z}}$ $\newcommand{\Aa}{\mathcal{A}}$ $\newcommand{\Bb}{\mathcal{B}}$ $\newcommand{\Cc}{\mathcal{C}}$ $\newcommand{\Dd}{\mathcal{D}}$ $\newcommand{\Ee}{\mathcal{E}}$ $\newcommand{\Ff}{\mathcal{F}}$ $\newcommand{\Gg}{\mathcal{G}}$ $\newcommand{\Hh}{\mathcal{H}}$ $\newcommand{\Ii}{\mathcal{I}}$ $\newcommand{\Jj}{\mathcal{J}}$ $\newcommand{\Kk}{\mathcal{K}}$ $\newcommand{\Ll}{\mathcal{L}}$ $\newcommand{\Mm}{\mathcal{M}}$ $\newcommand{\Nn}{\mathcal{N}}$ $\newcommand{\Oo}{\mathcal{O}}$ $\newcommand{\Pp}{\mathcal{P}}$ $\newcommand{\Qq}{\mathcal{Q}}$ $\newcommand{\Rr}{\mathcal{R}}$ $\newcommand{\Ss}{\mathcal{S}}$ $\newcommand{\Tt}{\mathcal{T}}$ $\newcommand{\Uu}{\mathcal{U}}$ $\newcommand{\Vv}{\mathcal{V}}$ $\newcommand{\Ww}{\mathcal{W}}$ $\newcommand{\Xx}{\mathcal{X}}$ $\newcommand{\Yy}{\mathcal{Y}}$ $\newcommand{\Zz}{\mathcal{Z}}$ $\newcommand{\bu}{\bullet}$ $\DeclareMathOperator{\id}{id}$
Why I don’t like Python as much as some other people do: Part 1 of ∞ | Rene Recktenwald Why I don't like Python as much as some other people do: Part 1 of ∞

Rene Recktenwald

GitHub
LinkedIn

Home
Blog

Why I don't like Python as much as some other people do: Part 1 of ∞

I

I have encountered what seemed like a bug at the time, and then stumbled upon this question on StackOverflow.

Consider the following piece of code

def foo(val,a=[]):
    a.append(val)
    return a

foo(1) # [1]
foo(2) # [1,2]
foo(3) # [1,2,3]
# Some big code block, 
foo(18) # ???

Without checking foo.__defaults__, it is pretty much impossible to predict the outcome of the function.

Reading through some of the answers in the above question, I can understand that there are reasons you would want mutable default arguments, but nevertheless, to me, I’m not a fan of that.