Re: Keyword arguments - strange behaviour?

2005-01-05 Thread brian . bird
Thanks. In case anyone else is looking, the recipe is at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303440 (which also shows how to make it work with python2.3 and below since they don't support decorators) Brian -- http://mail.python.org/mailman/listinfo/python-list

Keyword arguments - strange behaviour?

2004-12-21 Thread brian . bird
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=[]): ... bits.append(n) ... print bits ... >>> method(1) [1] >>> method(2) [1, 2] It's the same in python 1.5, 2.3 and 2.4 so it's not a bug. But I expected the variable "bits" to be re-initialised

Re: Keyword arguments - strange behaviour?

2004-12-21 Thread brian . bird
Thanks, this makes perfect sense. The phrase which sums it up neatly is "Default parameter values are evaluated when the function definition is executed" However, is there a good reason why default parameters aren't evaluated as the function is called? (apart from efficiency and backwards compatib

Re: Keyword arguments - strange behaviour?

2004-12-21 Thread brian . bird
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 a

Re: Keyword arguments - strange behaviour?

2004-12-23 Thread brian . bird
> Channelling the effbot, I think he was asking what namespace context you > expected the expression "arg=otherfunction(x)" to be evaluated in when > it's used at the time of a function call to dynamically create a new > default value for arg. Thanks, I realise now that's what was meant. I think I