[Python-ideas] 'where' statement in Python?

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Thu Jul 22 09:48:14 CEST 2010


On Wed, Jul 21, 2010 at 6:34 PM, Chris Rebert <pyideas at rebertia.com> wrote:


> funcs = []
> for i in range(5):
>     def f(i=i):
>        print("#", i)
>    funcs.append(f)
>
> They're all non-obvious idioms, but at least this one is short and
> easily recognized.

I actually had to read that twice before I recognized what you had
done, and I knew to look for something out of the ordinary. That said,
it *is* the solution GvR recommended as the best solution the last
time this came up. I just never understood why. To me, if you set
something as a default value for an argument, it should be because
it’s a default value, ie. a value that is usually one thing but can
also be set to something else at the caller’s discretion. I’m just not
comfortable with using the default value to mean “here’s a value you
should never change” or “pretty please, don’t pass in an argument,
because that will screw everything up” or even “I guess you could pass
in an argument if you wanted to, but that’s not a case I’m really very
busy thinking about". :-/ But maybe I’m in the minority on this one.

-- CJ



More information about the Python-ideas mailing list