2 ms·
You just need to fully understand when Python does evaluation. import types def function(item, stuff = lambda: []): if type(stuff) == types.Fun
by riobard 16y ago
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.