Linked Questions
1,313 questions linked to/from "Least Astonishment" and the Mutable Default Argument
243
votes
2
answers
126k
views
Why is the empty dictionary a dangerous default value in Python? [duplicate]
I put a dict as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better ...
164
votes
4
answers
301k
views
Python constructor and default value [duplicate]
Somehow, in the Node class below, the wordList and adjacencyList variable is shared between all instances of Node.
>>> class Node:
... def __init__(self, wordList = [], adjacencyList = []...
44
votes
9
answers
8k
views
Why are default arguments evaluated at definition time? [duplicate]
I had a very difficult time with understanding the root cause of a problem in an algorithm. Then, by simplifying the functions step by step I found out that evaluation of default arguments in Python ...
52
votes
5
answers
11k
views
python function default parameter is evaluated only once? [duplicate]
I am a python beginner, reading 'python tutorial', it says if we have a function:
def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
This will print
[1]
[1, 2]
[1, 2, 3]...
47
votes
2
answers
57k
views
Optional parameters in functions and their mutable default values [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm kind of confused about how optional parameters work in Python functions/methods.
I have the ...
27
votes
8
answers
3k
views
Why the "mutable default argument fix" syntax is so ugly, asks python newbie [duplicate]
Now following my series of "python newbie questions" and based on another question.
Prerogative
Go to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-...
18
votes
4
answers
32k
views
Python list should be empty on class instance initialisation, but it's not. Why? [duplicate]
I would like to create instances of a class containing a list that's empty by default; instead of later setting this list to the final full list I would like to successively add items to it. Here's a ...
25
votes
6
answers
6k
views
Python class function default variables are class objects? [duplicate]
I was writing some code this afternoon, and stumbled across a bug in my code. I noticed that the default values for one of my newly created objects was carrying over from another object! For example:
...
15
votes
2
answers
22k
views
Python class constructor with default arguments [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Can anyone explain the following strange behaviour?
I have the following class:
class Zoo:
def ...
21
votes
2
answers
17k
views
Python: Dictionary as instance variable [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm very confused about the behavior of dictionaries as class instance variables in Python 3. The way ...
16
votes
2
answers
13k
views
Constructor does weird things with optional parameters [duplicate]
Possible Duplicate:
least astonishment in python: the mutable default argument
I want to understand of the behavior and implications of the python __init__ constructor. It seems like when there ...
16
votes
2
answers
4k
views
Python base classes share attributes? [duplicate]
Code in test.py:
class Base(object):
def __init__(self, l=[]):
self.l = l
def add(self, num):
self.l.append(num)
def remove(self, num):
self.l.remove(num)
class ...
5
votes
4
answers
1k
views
Python function calls are bleeding scope, stateful, failing to initialize parameters? [duplicate]
Before I have the audacity to file a bug report, I thought I'd check my assumptions among wiser Pythonistas here. I encountered a baffling case today, so I whittled it down to a toy example, shown ...
11
votes
5
answers
7k
views
Difference between mutation, rebinding, copying value, and assignment operator [duplicate]
#!/usr/bin/env python3.2
def f1(a, l=[]):
l.append(a)
return(l)
print(f1(1))
print(f1(1))
print(f1(1))
def f2(a, b=1):
b = b + 1
return(a+b)
print(f2(1))
print(f2(1))
print(f2(1))
...
11
votes
3
answers
6k
views
Default value in a function in Python [duplicate]
I am noticing the following:
class c:
def __init__(self, data=[]):
self._data=data
a=c()
b=c()
a._data.append(1)
print b._data
[1]
Is this the correct behavior?