News for hackers. Stuff that matters.
Re:Python's Mutable Default Problem by riobard on Sunday February 20, 2011 @01:03PM (#2242510)
You just need to fully understand when Python does evaluation.

    import types
    def function(item, stuff = lambda: []):
        if type(stuff) == types.FunctionType:
            stuff = stuff()
        stuff.append(item)
        print stuff

    function(1)
    # prints '[1]'

    function(2)
    # prints '[2]'

In Scala it has a better syntax because of typed function object:

    trait Map[A, B] {
        …
        def getOrElse (key: A, default: ⇒ B): B
        …
    }

the `default` parameter is a function, so when you do

    getOrElse(someKey, defaultValue)
The `defaultValue` becomes a function that generates the value you put there when called.

There are no comments on this comment yet.