[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Zen of Python



On Tue, 28 May 2002, Fredrik Lundh wrote:

> Avi Bryant wrote:
> > What's the equivalent Python idiom?
>
> d.setdefault("spam", 42)

Not the same thing at all, unless you happen to be using a constant value.
Perhaps my example should have been

d at: 'spam' ifAbsentPut: [x * 42]

> if d.has_key("spam"):
>     d["spam"] = 42

I assume you mean "unless", not "if".  Nothing much wrong with this, I
agree, but it's a statement, not an expression.  The argument will be that
having to use extra temps is never bad because it always makes things more
readable anyway, which I don't necessarily agree with, but fair enough.

> or, if you insist:
>
> if_absent_put(d, "spam", lambda: 42)

Actually I'd prefer d.at("spam", if_absent_put=lambda: 42).  Which is a
shame because it doesn't correspond nicely to the [] operator.  Is
d["spam", if_absent_put=lambda:42] legal?

But that's clearly not actually idiomatic Python.  This is a discussion
about culture as much as anything, isn't it?

> > My guess is that it will be either less regular, or less explicit,
> > or both.
>
> whatever.

Whatever indeed.  This whole thread is turning into a language pissing
match.  Count me out.