default-parameters

Workaround Mutable Default Arguments in Python

Workaround Mutable Default Arguments in Python Question: Going through the python documentation, I came across below. Important warning: The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed …

Total answers: 1

Python, default keyword arguments after variable length positional arguments

Python, default keyword arguments after variable length positional arguments Question: I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I’m writing with the following “get” method, for example: class Foo(object): def __init__(self): print “You have created …

Total answers: 2

"Least Astonishment" and the Mutable Default Argument

"Least Astonishment" and the Mutable Default Argument Question: Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function called with no parameter to always return a list with only one element: [5]. The result is instead very …

Total answers: 34