Keyword arguments - strange behaviour?
harold.fellermann at upf.edu
Tue Dec 21 16:26:22 CET 2004
I cannot see any strange behavior. this code works exacly as you and I
>>> def otherfunction(x) :
... return x
>>> def function(arg=otherfunction(5)) :
... return arg
Or is this not what you excepted?
- harold -
On 21.12.2004, at 15:47, brian.bird at securetrading.com wrote:
> def function(arg=otherfunction(value)):
> return arg
> My expectation would have been that otherfunction(value) would be
> called if (and only if) the arg keyword parameter was missing from the
> function() call (ie. the optional value is evaluated the lazy way).
> Also, otherfunction would be called each and every time this function()
> is called without the arg keyword. (At least, I would have assumed this
> before today)
> Still, I can see why it's been implemented the way it has, it just
> seems a shame there isn't a neat shortcut to default lots of optional
> arguments to new mutable objects. And since I'm not the only one to
> fall into this trap it makes me wonder why the default behaviour isn't
> made to be what most people seem to expect?
Freunde, nur Mut,
Lächelt und sprecht:
Die Menschen sind gut --
Bloß die Leute sind schlecht.
-- Erich Kästner
More information about the Python-list