Keyword arguments - strange behaviour?
Steven Bethard
steven.bethard at gmail.com
Thu Dec 23 11:21:24 EST 2004
Fuzzyman wrote:
>>>Steven Bethard wrote:
>>>>
>>>>So, one of my really common use cases that takes advantage of the
>>>>fact that default parameters are evaluated at function definition
>>>>time:
>>>>
>>>>def foo(bar, baz, matcher=re.compile(r'...')):
>>>> ...
>>>> text = matcher.sub(r'...', text)
>>>> ...
> Sure.. but you also gave an example of an alternative that was complex,
Interesting. I would have thought that my example was pretty simple.
Maybe it would be helpful to generalize it to:
def foo(bar, baz, spam=badger(x, y, z)):
...
All it does is use a default value that was produced by a function call.
I'm surprised you haven't run into this situation before...
Of course, what is complex or simple is a matter of personal opinion. I
use this pattern so often that it's quite simple to me, but I guess I
can understand that if you don't use such a pattern, it might seem
foreign to you.
Steve
More information about the Python-list
mailing list