September 19, 2006

Python 2.5 is out!

Python
So, Python 2.5 is finaly out!

I'm not coding much in Python this days, specially not using Python to do real stuff (just some simple scripts), but you have to love this new release.

Besides being faster and more reliable, it has some awesome new language stuff, like the new "with" operator: it will turn try/finally blocks from
        VAR = EXPR
        VAR.__enter__()
        try:
            BLOCK
        finally:
            VAR.__exit__()

into
        with VAR = EXPR:
            BLOCK

... Awesome, heh?

Also, generators now have push, throw and close methods. And this is to Perl coders like me: I also liked the fact that Perl lets you do stuff like
$a = 3 if $b == 1;

, but Python 2.5 will let you do not only that, but
$a = 3 if $b == 1 else $a = 2

!!!

Finally, let me quote two other things that left me excited:

New builtins any and all evaluate whether an iterator contains any or all True values, respectively.

min and max gained a key keyword parameter, analogous to sort.

2 comments:

  1. $a = 3 if $b == 1 else $a = 2
    That's insane! It starts as a postfix notation ($a = 3 if $b == 1) then it passes to a prefix notation (else $a = 2).
    I think postfix notation, in all languages that use them, needs to be better worked. We say something like "a equals three if b equals 1, a equals two otherwise". So a reserved keyword is missing... The "otherwise"! :)
    In the future we'll write:
    $a = 3 if $b == 1,
    $a = 2 otherwise;

    It seems nasty... Specially when we can do it this way:
    $a = ($b == 1 ? 3 : 2);
    (Quite similar to an infix notation, isn't it?)

    ReplyDelete
  2. Not insane, just unusual. But - see - while I like the '?' operand you have to agree that

    (truevalue if condition else falsevalue)

    is more readable than

    "condition ? truevalue : falsevalue"

    ...

    ReplyDelete