473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected default arguments behaviour (Maybe bug?)

Hi, 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 UNIX.

Thanks in advance!
Jul 13 '08 #1
5 939
sukkop...@gmail .com napisa³(a):
Hi, 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 UNIX.

Thanks in advance!
I expect it's because default values for parameters are evaluated and
bound at definition time. So once "def m (self, param = a):" line
executes, the default value for parameter is forever bound to be 1.
What you can do is for example:
def m (self, param = None):
if param is None: param = self.a
print "param = %d" % param
Regards,
Marek
Jul 13 '08 #2
On 13 Lug, 19:42, marek.ro...@wp. pl wrote:
I expect it's because default values for parameters are evaluated and
bound at definition time. So once "def m (self, param = a):" line
executes, the default value for parameter is forever bound to be 1.
What you can do is for example:
Yes, that's what I thought, too. Although, it does not seem to me the
correct thing that has to be done, that is why I reported it.

Also thanks for your suggestion, that might work, even though I
already have implemented a workaround (two different methods).

Regards!
Jul 13 '08 #3
FYI, I have opened a bug on the official tracker: http://bugs.python.org/issue3403.
Jul 18 '08 #4
su*******@gmail .com <su*******@gmai l.com>:
On 13 Lug, 19:42, marek.ro...@wp. pl wrote:
>I expect it's because default values for parameters are evaluated and
bound at definition time. So once "def m (self, param = a):" line
executes, the default value for parameter is forever bound to be 1.
What you can do is for example:

Yes, that's what I thought, too. Although, it does not seem to me the
correct thing that has to be done, that is why I reported it.
It _is_ the correct thing. Evaluation of default parameters at "declaratio n
time" and not at invocation is truely a language feature, not a bug.

You'll find your bug report being closed quickly.

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jul 18 '08 #5
On 18 Lug, 13:23, "Sebastian \"lunar\" Wiesner"
<basti.wies...@ gmx.netwrote:
It _is_ the correct thing. *Evaluation of default parameters at "declaratio n
time" and not at invocation is truely a language feature, not a bug.

You'll find your bug report being closed quickly.
It has ;). I had totally missed the tutorial section where it is
stated, which actually even has an example quite close to the one I
provided. Sorry for the waste of time.

Giorgio

Jul 18 '08 #6

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

Similar topics

14
5227
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while for keyword arguments the tutorial shows: def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
8
6654
by: Agent Mulder | last post by:
Hi group, I want to know why this doesn't work void f(int a=0,int b=1,int c=2){} int main() { f(); //OK f(1); //OK f(1,2); //OK f(1,2,3); //OK
11
2598
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5 )),
12
7846
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
35
2203
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with code. This example is trivial and has design issues, but it demonstrates a problem I've seen in production systems: def main(argv = ): 'print out arguments with BEGIN and END' argv.insert(1, "BEGIN")
3
1252
by: burgermeister01 | last post by:
Default Arguments: what's the general consensus about them? To give an example, so we're all on the same page, this is the syntax I'm talking about: function foo($bar = "some value"){ } So, in the past when I was programming with PHP a lot, I found myself using default arguments more and more because they were so convenient.
24
2498
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread finally, and painfully, fade away than another one starts up. I suggest that Python should raise warnings.RuntimeWarning (or similar?) when a function is defined with a default argument consisting of a list, dict or set. (This is not meant as an...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8262
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8252
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3875
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.