Unexpected default arguments behaviour (Maybe bug?)

S

sukkopera

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!
 
M

marek.rocki

(e-mail address removed) 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
 
S

sukkopera

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!
 
S

Sebastian \lunar\ Wiesner

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 "declaration
time" and not at invocation is truely a language feature, not a bug.

You'll find your bug report being closed quickly.
 
S

sukkopera

It _is_ the correct thing.  Evaluation of default parameters at "declaration
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
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top