homepage

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: Unexpected default arguments behaviour
Type: behavior Stage:
Components: None Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SukkoPera, georg.brandl
Priority: normal Keywords:

Created on 2008年07月18日 08:41 by SukkoPera, last changed 2022年04月11日 14:56 by admin. This issue is now closed.

Messages (2)
msg69943 - (view) Author: (SukkoPera) Date: 2008年07月18日 08:41
I have just encountered a Python behaviour I wouldn't expect. Take
the following code:
------------------------------------------------------------------------
class Parent:
 a = 1
 def m (self, param = a):
 print "param = %d" % param
class Child (Parent):
 a = 2
p = Parent ()
p.m ()
c = Child ()
c.m ()
------------------------------------------------------------------------
I would expect to receive the following output:
param = 1
param = 2
But actually I get:
param = 1
param = 1
Is this the correct behaviour, and then why, or is it a bug? For
reference, I am using Python 2.5.1 on GNU/Linux.
There has been a short discussion about this at
http://groups.google.it/group/comp.lang.python/browse_thread/thread/9f740eea131e7ef2/56fd4e120a069a1d#56fd4e120a069a1d.
msg69944 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008年07月18日 09:01
This is another "problem" due to the fact that parameter defaults are
evaluated once during function definition, not every time the function
is called. This is expected and will not change.
History
Date User Action Args
2022年04月11日 14:56:36adminsetgithub: 47653
2008年07月18日 09:01:09georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg69944
nosy: + georg.brandl
2008年07月18日 08:41:35SukkoPeracreate

AltStyle によって変換されたページ (->オリジナル) /