From the Python's tutorial, about default argument values:
<quote>
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 to it on subsequent calls:
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]
If you don't want the default to be shared between subsequent calls, you can
write the function like this instead:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
</quote>
I can't imagine how subsequent calls to f can share the same value for L.
The tutorial says that a new symbol table for the variables inside of the
function is created each time the function is called, and I would say the
symbol table is destructed when the function finishes execution.
How is the value of L conserved between funtion calls?
Can someone explain the mechanism to me?
Thanks,
Tito 3 2823
Default values that are mutable types are evaluated
at definition time not execution time. That means
L is set to empty list when Python parses function
and it lives outside the function definition. It
is rather 'different' but I can assure you that is
how it works and it can sometimes bite programmers
new to Python that overlook the docs you refer to.
HTH,
Larry Bates
Syscon, Inc.
"Tito" <ti***************************@inicia.es> wrote in message
news:2l************@uni-berlin.de... From the Python's tutorial, about default argument values:
<quote> 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 to it on subsequent calls: 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]
If you don't want the default to be shared between subsequent calls, you
can write the function like this instead: def f(a, L=None): if L is None: L = [] L.append(a) return L </quote>
I can't imagine how subsequent calls to f can share the same value for L. The tutorial says that a new symbol table for the variables inside of the function is created each time the function is called, and I would say the symbol table is destructed when the function finishes execution.
How is the value of L conserved between funtion calls? Can someone explain the mechanism to me?
Thanks, Tito
Tito wrote: How is the value of L conserved between funtion calls? Can someone explain the mechanism to me?
A function is just a callable object. Its default arguments are stored as a
tuple in the func_defaults attribute. You can easily experiment with these
kind of things in the interpreter: def f(a, items=[]):
.... items.append(a)
.... f(1) f(2) f(3) f.func_defaults
([1, 2, 3],) f(4) f.func_defaults
([1, 2, 3, 4],)
Peter
Thanks to you both for your explanation.
Tito This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Panayotopoulos |
last post by:
Hello all,
Maybe I'm being foolish, but I just don't understand why the following
code behaves as it does:
- = - = - = -
class listHolder:
def __init__( self, myList= ):
self.myList =...
|
by: Frank Bechmann |
last post by:
Eventually most of you will not learn much from this because it's just
another event in the 'default argument value gotcha' series, but
because it cost me some hours yesterday to spot this 'error'...
|
by: Nick Coghlan |
last post by:
Time for another random syntax idea. . .
So, I was tinkering in the interactive interpreter, and came up with the
following one-size-fits-most default argument hack:
Py> x = 1
Py> def...
|
by: Dave Vandervies |
last post by:
If I feed this to g++:
--------
int foo(int i=42);
int foo(int i=42)
{
return i;
}
--------
It says (with -W -Wall -ansi -pedantic):
|
by: Capstar |
last post by:
Hi NG,
I am trying to get the attached piece of code to work, but I can't
figure out what I'm doing wrong. To me it seems that when I don't pass
an argument to x::do_something, it should use the...
|
by: netvaibhav |
last post by:
Hi All:
Here's a piece of Python code and it's output. The output that Python
shows is not as per my expectation. Hope someone can explain to me this
behaviour:
class MyClass:
def...
|
by: aaragon |
last post by:
Hello all. I have a simple question that seems trivial but I can't
make it to work. I have a class that takes as a template argument,
another class. The idea is as follows:
#include...
|
by: William Xu |
last post by:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv)
{}
gcc complains:
|
by: kenneth |
last post by:
Dear all,
I have encountered this weird problem.
I have a class definition with an __init__ argument 'd'
which defaults to {}. This argument is put in the 'self.d'
attribute at initialization...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |