PEP 308 original proposal "used" already in Python Documentation

Jon Perez jbperez808 at yahoo.com
Wed Mar 5 02:00:27 EST 2003


"Christoph Becker-Freyseng" <webmaster at beyond-thoughts.com> wrote:

> Hello,
>
> I just found something funny in Python's Documentation.
> In 2.1.6 Mapping Types you can find the following:
>
> a.get(k[, x])            a[k] if k in a, else x     (4)
> a.setdefault(k[, x])     a[k] if k in a, else x     (also setting it)  (5)
>
> It looks very similar to the original proposalof PEP 308, but it's the
> explanation of {}.get and {}.setdefault .

The ternary operator would definitely be a more intuitive
way to get the same behaviour as get() (albeit longer -
but we're not Perlistas are we?).

[a[k] if k in a else x]

Now how would we express setdefault() using the ternary
operator though?I don't think it is legal to have multiple
statements in a single line if/else contruct in Python:

[a[k] if k in a else a[k]=x: x]

or

[a[k] if k in a else a[k]=x: return x]

wouldn't work.






More information about the Python-list mailing list