473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Bug in python!? persistent value of an optional parameter in function!

Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:

call1: ['sdf']
call2: ['Set within test for call2']
call3: ['Set within test for call2']

instead of what I should get,

call1: ['sdf']
call2: ['Set within test for call2']
call3: ['Set within test for call3']

I'm using Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02). The code is
below.
#!/usr/bin/python

def testPersistence(anarg,twooption=[]):
#print anarg
if not twooption:
twooption.append('Set within test for '+anarg)
print anarg +': '+str(twooption)

testPersistence('call1',twooption=['sdf']);
testPersistence('call2');
testPersistence('call3');

Mar 8 '07 #1
12 1437
"C Barr Leigh" <cp********@gmail.comwrites:
Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:
It is intentional, not a bug, see the docs. Whether it's desirable is
a different question.
Mar 8 '07 #2
En Wed, 07 Mar 2007 23:39:21 -0300, C Barr Leigh <cp********@gmail.com>
escribió:
Help! Have I found a serious bug?
Not at all! This is by design.
def testPersistence(anarg,twooption=[]):
#print anarg
if not twooption:
twooption.append('Set within test for '+anarg)
See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm

--
Gabriel Genellina

Mar 8 '07 #3
Paul Rubin wrote:
"C Barr Leigh" <cp********@gmail.comwrites:
>>Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:


It is intentional, not a bug, see the docs. Whether it's desirable is
a different question.
True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior
is silly.

John Nagle
Mar 8 '07 #4
John Nagle <na***@animats.comwrites:
True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior is silly.
That would be the worst of both worlds. The main alternative to the
present behavior is re-computing the default value every time the
function is entered.
Mar 8 '07 #5
On Mar 7, 10:14 pm, John Nagle <n...@animats.comwrote:
Paul Rubin wrote:
"C Barr Leigh" <cpblPub...@gmail.comwrites:
>Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:
It is intentional, not a bug, see the docs. Whether it's desirable is
a different question.

True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior
is silly.
1. If you'd given it a little more thought than it took you to write
this, you would perhaps realize that it is in general impossible to
determine automatically whether an arbitrary object is mutable or
not.

2. Even if you could determine it, it would be overly restrictive to
disallow all mutable values from defaults. The fact that an object is
mutable doesn't mean that the function will try to mutate it:

def paintWall(ind, colormap={1:'red', 2:'blue', 5:'green'}):
print "Let's paint the wall %s" % colormap[ind]
George

Mar 8 '07 #6
On Mar 7, 10:24 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
John Nagle <n...@animats.comwrites:
True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior is silly.

That would be the worst of both worlds. The main alternative to the
present behavior is re-computing the default value every time the
function is entered.
One can do this is today's Python if he's so inclined, albeit with a
more verbose syntax: http://aspn.activestate.com/ASPN/Coo.../Recipe/502206

George

Mar 8 '07 #7
On Thu, 08 Mar 2007 03:14:53 +0000, John Nagle wrote:
Paul Rubin wrote:
>"C Barr Leigh" <cp********@gmail.comwrites:
>>>Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:


It is intentional, not a bug, see the docs. Whether it's desirable is
a different question.

True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior
is silly.
It's just *different*, not silly.

It's also quite useful in some circumstances, e.g. cheap caching without
using a global variable.

def factorial(n, _cache={}):
try:
return _cache[n]
except KeyError:
# fall back to slow calculation
if n <= 1:
result = 1
else:
result = factorial(n-1)*n
_cache[n] = result
return result

There are other ways of implementing caches, but this is quick and easy
and works well for many functions.
--
Steven D'Aprano

Mar 8 '07 #8
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrites:
def factorial(n, _cache={}):
try:
return _cache[n]
except KeyError:
There are other ways of implementing caches, but this is quick and easy
and works well for many functions.
I like this better (examples are untested):

def factorial(n):
try:
return factorial.cache[n]
except KeyError: pass
...
factorial.cache = {}

you could even use a decorator:

def memoize(f): # memoize a 1-arg function
f.cache = {}
def g(f,x):
if x in f.cache: return f.cache[x]
return f.cache.setdefault(x, f(x))
return functools.partial(g,f)
...

@memoize
def factorial(n):
return 1 if n==0 else n*factorial(n-1)
Mar 8 '07 #9
Oh, oops! Of course... :) A great and sensible feature if you're
expecting it.
Thanks very much, everyone, for the links and discussion!

Chris

Mar 8 '07 #10
John Nagle wrote:
True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior
is silly.
Why? You're free to only use immutables.

Regards,
Björn

--
BOFH excuse #42:

spaghetti cable cause packet failure

Mar 8 '07 #11
C Barr Leigh a écrit :
Help! Have I found a serious bug?
No. This is a FAQ. Default arguments of functions are evaled only once -
when the def statement is eval'd and the function object constructed.
This seems like highly undesired behaviour to me.
Possibly, but this is unlikely to change in a near future, so you'd
better get used to it. If you want mutable objects as default values for
functions, the idiom is:

def some_func(default=None):
if default is None:
default = []
# code here

HTH
Mar 8 '07 #12
"Gabriel Genellina" typed:
>
See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm
Thanks for the link, Gabriel. I didn't know about this.

--
Ayaz Ahmed Khan

Falling in love makes smoking pot all day look like the ultimate in
restraint.
-- Dave Sim, author of "Cerebus".
Mar 8 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Emile van Sebille | last post by:
QOTW (advanced interfaces track): "I'm firmly in favour of any language that can DWIMNWIS." -- Tim Delaney QOTW (MS roadkill track): "Underestimate MS at your own risk. It is one thing to not...
2
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 240 open ( -1) / 2655 closed (+15) / 2895 total (+14) Bugs : 766 open ( +0) / 4514 closed (+22) / 5280 total (+22) RFE : 155 open...
0
by: Cliff Wells | last post by:
QOTW: "I find it hard to believe nobody has tried this yet, unless it's the case that those who know how to do it also know that it's actually not possible for some reason..." -- Peter Hansen...
1
by: Cliff Wells | last post by:
QOTW: "I find it hard to believe nobody has tried this yet, unless it's the case that those who know how to do it also know that it's actually not possible for some reason..." -- Peter Hansen...
0
by: Josiah Carlson | last post by:
QOTW: "I still think this is a silly idea, but at least it doesn't track mud all over Python's nice clean rugs." -- Michael J. Fromberger...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.