This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: default param values
Type: behavior Stage:
Components: Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, georg.brandl, vladimir
Priority: normal Keywords:

Created on 2010-08-27 22:31 by vladimir, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg115137 - (view) Author: vladimir (vladimir) Date: 2010-08-27 22:31
This program:

class t( object):
	def __init__(self,ime,param1=[],param2=[]):
		print "+++init+++"
		print "ime = ",ime
		print "param1 = ", param1
		print "param2 = ", param2
		print
		self.ime = ime
		self.sirina = param1
		self.visina = param2
	def load(self):
		print "load self.ime = ",self.ime
		self.sirina.append('a')
		self.visina.append('b')


t1 = t("jedan",[1,2],[3,4])
t2 = t("dva",[5,6],[7,8])
t3 = t("tri")
t3.load()
t4 = t("cetiri")

produces this output:

+++init+++
ime =  jedan
param1 =  [1, 2]
param2 =  [3, 4]

+++init+++
ime =  dva
param1 =  [5, 6]
param2 =  [7, 8]

+++init+++
ime =  tri
param1 =  []
param2 =  []

load self.ime =  tri
+++init+++
ime =  cetiri
param1 =  ['a']
param2 =  ['b']



Question:

	How's  possible that call t4 = t("cetiri")
	gives param1 value ['a'] and
	      param2 value ['b']

Is it possible that is bug or my computer gone crazy or maybe i am

executed on:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
msg115138 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-08-27 22:34
This is by design.  Parameter default values are evaluated when the function is defined, not when it is called.  Therefore, by assigning the default value to an instance attribute, you are basically sharing it between instances.
msg115168 - (view) Author: vladimir (vladimir) Date: 2010-08-28 20:18
On Sat, Aug 28, 2010 at 12:34 AM, Georg Brandl <report@bugs.python.org>wrote:

changes parameter value. It' would be understandable, as exmple in
documentation with acumulating value of L, that changing parameter in side
class affect default
    value in next call. But this is out of mind. Did you try to execute
program on your computer? Does it give same output? I rather consider it
bug. Languages like python should be semanticly understandable, without
knowing how
    something works. I read that van Rossum working on it ( clearing
something  ) in version 3.0, did you try it? Does verison 3.0 correct this?
msg115169 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-08-28 20:24
Rest assured: it's expected behavior.
msg115199 - (view) Author: vladimir (vladimir) Date: 2010-08-30 02:33
On Sat, Aug 28, 2010 at 10:24 PM, Benjamin Peterson
<report@bugs.python.org>wrote:

>
> Benjamin Peterson <benjamin@python.org> added the comment:
>
> Rest assured: it's expected behavior.
>
> Thank you for your cooperation.
History
Date User Action Args
2022-04-11 14:57:05adminsetgithub: 53912
2010-08-30 02:34:21eric.araujosetfiles: - unnamed
2010-08-30 02:34:18eric.araujosetfiles: - unnamed
2010-08-30 02:33:15vladimirsetfiles: + unnamed

messages: + msg115199
2010-08-28 20:24:44benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg115169
2010-08-28 20:18:14vladimirsetfiles: + unnamed

messages: + msg115168
2010-08-27 22:34:13georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg115138

resolution: not a bug
2010-08-27 22:31:06vladimircreate