Calling a constructor in a default argument.

Bram Stolk bram at nospam.sara.nl
Mon Mar 8 08:42:03 EST 2004


Hello,


I wrote a function that takes a default argument.
The default arg is set to a constructor call.

I expected this constructor to be called every time I call the func
without the arg.

However, it turns out that the constructor is called only once.
Is this the desired behaviour, or a known bug?

The output of the prog below is:
Set([88, 89])
Set([88, 89, 100, 101])

Whereas I expected:
Set([88, 89])
Set([100, 101])

Thanks,

   Bram

#!/usr/bin/python -i
import sys
import sets
import whrandom


def foo(lst, set=sets.Set()) :
  for i in lst :
    set.add(i)
  return set

print foo([88, 88,89])

print foo([100, 100,101])

-- 
------------------------------------------------------------------------------
 Bram Stolk, VR Specialist.
 SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP  AMSTERDAM
 email: bram at nospam.sara.nl   Phone +31-20-5923059  Fax +31-20-6683167

"For the costs of subsidized agriculture in the EU, we can have all 56 million
 European cows fly around the world. First Class." - J. Norberg
------------------------------------------------------------------------------



More information about the Python-list mailing list