defaults for function arguments bound only once(??)

H

horos11

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]
 
C

Chris Rebert

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/controlflow.html#default-argument-values
Essentially, use None as the default value, then check for it and
assign the real default value in the function body.

Cheers,
Chris
 
S

Simon Forman

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/general/#why-are-default-values-shared-between-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/functions.html#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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top