What is a function parameter =[] for?

Marko Rauhamaa marko at pacujo.net
Thu Nov 19 13:39:38 EST 2015


BartC <bc at freeuk.com>:

> Yes. In the languages I create, pretty much everything is mutable,
> provided it can supply an l-value. Constructs such as those for empty
> lists ([] in Python, () in mine) aren't l-values.
>
> But it doesn't apply default values.

Python's default-value semantics is analogous to C++:

========================================================================
#include <iostream>

using namespace std;

static int z;

static void f(int &x = z)
{
    x++;
}

int main()
{
    cout << z << endl;
    f();
    cout << z << endl;
    f();
    cout << z << endl;
    return 0;
}
========================================================================

which prints:

    0
    1
    2


Marko



More information about the Python-list mailing list