Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > defaults for function arguments bound only once(??)

Reply
Thread Tools

defaults for function arguments bound only once(??)

 
 
horos11
Guest
Posts: n/a
 
      10-04-2009
All,

Another one, this time a bit shorter.

It looks like defaults for arguments are only bound once, and every
subsequent call reuses the first reference created. Hence the
following will print '[10,2]' instead of the expected '[1,2]'.

Now my question - exactly why is 'default_me()' only called once, on
the construction of the first object? And what's the best way to get
around this if you want to have a default for an argument which so
happens to be a reference or a new object?

---- code begins here ---

import copy
class A:

def default_me():
return [1,2]

def __init__(self, _arg=default_me()):
self.arg = _a


a = A()
a.arg[0] = 10
b = A()

print b.arg # prints [10,2]
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-04-2009
On Sat, Oct 3, 2009 at 11:29 PM, horos11 <(E-Mail Removed)> wrote:
> All,
>
> Another one, this time a bit shorter.
>
> It looks like defaults for arguments are only bound once, and every
> subsequent call reuses the first reference created. Hence the
> following will print '[10,2]' instead of the expected '[1,2]'.
>
> Now my question - exactly why is 'default_me()' only called once, on
> the construction of the first object?


Actually, the single call happens at the "definition-time" of the
function. As for why, it's less magical than re-evaulating it on every
function call when that parameter is not supplied a value; trust me,
the issue has been argued to death.

> And what's the best way to get
> around this if you want to have a default for an argument which so
> happens to be a reference or a new object?


See http://docs.python.org/tutorial/cont...rgument-values
Essentially, use None as the default value, then check for it and
assign the real default value in the function body.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      10-04-2009
On Sun, Oct 4, 2009 at 2:29 AM, horos11 <(E-Mail Removed)> wrote:
> All,
>
> Another one, this time a bit shorter.
>
> It looks like defaults for arguments are only bound once, and every
> subsequent call reuses the first reference created. Hence the
> following will print '[10,2]' instead of the expected '[1,2]'.
>
> Now my question - exactly why is 'default_me()' only called once, on
> the construction of the first object? And what's the best way to get
> around this if you want to have a default for an argument which so
> happens to be a reference or a new object?


This is a FAQ: http://www.python.org/doc/faq/genera...etween-objects

>
> ---- code begins here ---
>
> import copy
> class A:
>
> * *def default_me():
> * * * *return [1,2]
>
> * *def __init__(self, _arg=default_me()):
> * * * *self.arg = _a
>
>
> a = A()
> a.arg[0] = 10
> b = A()
>
> print b.arg *# prints [10,2]


Your code is weird: you import copy module but don't use it; you
define default_me() as a "sort of" static method
(http://docs.python.org/library/funct...l#staticmethod) but then
only use it to generate a default argument that has nothing to do with
the class you just defined; and in __init__() you use "_a" which isn't
defined anywhere in this code snippet.

What are you actually trying to accomplish?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lower bound & Upper bound sunil panda Java 9 10-07-2008 08:32 PM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
How to represent the data in a not bound control via bound control Mario Krsnic ASP .Net 0 06-23-2006 07:38 AM
named method arguments with defaults eliben@gmail.com Ruby 4 03-11-2006 03:37 PM
Getting lower-bound and upper-bound of strings input Rhiner Dan C++ 1 03-27-2005 02:03 AM



Advertisments