Slashdot Mirror


Python 2.4 Final Released

Eventh writes "The final release of Python 2.4 was just released. Python 2.4 is the result of almost 18 month's worth of work on top of Python 2.3. New features are, but not limited to, function decorators, generator expressions, a number of new module and more. Check out Andrew Kuchling's What's New In Python for a detailed view of some of the new features of Python 2.4. "

8 of 359 comments (clear)

  1. genexps by ultrabot · · Score: 5, Insightful

    You forgot the most important improvement, the "generator expressions".

    From the AMK's excellent (as always) overview:

    print sum(obj.count for obj in list_all_objects())

    The important part is that no intermediate list is generated, because we are dealing with generators.

    Generators in general kick so much ass it's not even funny.

    --
    Save your wrists today - switch to Dvorak
    1. Re:genexps by CoughDropAddict · · Score: 4, Insightful
      At a language level, it's true that generators don't have anything to do with lambda or code blocks. But when you consider what these features are used for, there is some overlap.

      Consider the example you gave:
      print sum(obj.count for obj in list_all_objects())
      In Ruby, you can accomplish the same thing by writing:
      print (objGenerator.map { |obj| obj.count } ).sum
      where "objGenerator" is an object that mixes in the "Enumerable" module. An even better way (that really avoids building an intermediate list, even of integers) is:
      a = 0
      objGenerator.each { |obj| a += obj.count }
      print a
      There is one thing that generators give you that blocks can't, that is very, very cool. With a generator, you can create programs with a pipeline architecture, where different steps of the pipeline all can be written as if they have the main loop.

      Imagine you are writing a compiler. You could write (be easy on my syntax, I haven't written Python in a while)
      def lexer(input):
      state = STATE_BEGIN
      ch = input.read(1)
      if state == STATE_BEGIN:
      if ch == '(':
      yield TOKEN_LEFT_PAREN
      (...)

      def parser(lexer)
      token = lexer.get()
      if token == TOKEN_LEFT_PAREN:
      yield parse_expr(lexer)
      # gobble TOKEN_LEFT_PAREN
      lexer.get()
      (...)
      The beauty is that both your lexer and your parser can be written as if they have the main loop, and all they have to do is yield a token/expression when they find one. In reality, it's pipelined and you get the efficiency of not having to build up the entire list of tokens before you start parsing.
  2. Re:I *want* to be enthused, but... by oexeo · · Score: 4, Insightful
    BTW, why would you want to get more proficient in C? Programmers are abandoning C in droves. It's just not programmer-time efficient to do things in C anymore. It's one thing if you are maintaining a project that was written in C originally, but for new projects, C is a non-starter.

    I think one of the problems is too many people are still spreading the myth that it's essential to learn C before moving on to C++, which is totally false, C++ is a language in itself, and can be treated as such. Learning C (unless you intend to use it) is a waste of time, and I would go so far to say learning C first will make you less producive in C++, because it teaches concepts which are not applicable, or are actually bad habits when used in C++. At least that's my view on the subject.

    That said, C still has its uses, but for many projects (like parent said) it's a "non-starter"

  3. Re:18 Months by MikeBabcock · · Score: 4, Insightful
    Its not that they're hard to parse, its that they're unnecessary and ugly.
    if (x == y)
    {
    if (!do(something))
    {
    printf(failuremsg);
    return -1;
    }
    return 0;
    }
    vs.
    if x == y:
    if not do(something):
    print failuremsg
    return -1
    return 0
    Linus' comments in the kernel's CodingStyle document are relevant too -- try to keep your functions so that you can read most of them on one screen. Python allows you to do this more often (I find) without resorting to strange brace positions.
    --
    - Michael T. Babcock (Yes, I blog)
  4. Re:Supprised by kirkjobsluder · · Score: 4, Insightful

    Can one tell me why I should learn python and not any other programming language anyway?

    Well, some of the things I like about python:
    1: tightly structured code.
    2: less punctuation.
    3: more readable syntax.
    4: full OOP from the ground up (in contrast to perl where the OOP is bolted on with references).
    5: short production cycle.

    Many of these things can be found in other languages as well. So it largely comes down a matter of preference.

    From the little I have seen, python seems to be a command line language. Is it anywhere similar to Visual Basic, which I have come to see and experience through a GUI?

    Check out tkinter and wxPython.

  5. Re:Is it just me? by pclminion · · Score: 4, Insightful
    If a newbie writes something that can be re-written with a list comprehension for instance, it's pretty much given that nobody will actually answer his question, choosing instead to re-write his program for him using tricks and shortcuts.

    List comprehensions are not "tricks," they are an extremely powerful language feature. Newbies should be taught how to use them, and translating their loop-based code into list comprehensions is probably the best way to do that.

    Python can be written from either an imperative standpoint or a pseudo-functional one. Most of the highly skilled Python programmers code in the pseudo-functional style, because it is more efficient and (arguably) more elegant.

    Sure, you can get into some pretty scary territory with various combinations of sum(), map(), reduce(), and list comprehensions but that's your choice. I admit that there should not be such a big performance gap between the different styles... This is due to not enough effort being spent on improving the VM.

  6. python - awesome by hashmap · · Score: 5, Insightful
    In some of my last projects I had to analyze a Perl and a Java program. I programmed a few years in both of these, but now after a year with Python I was truly suprised of how primitive these languages felt.

    All those funny symbols, casting back and forth in perl just getting in the way yet don't really say anything useful ... here is an example:

    foreach my $val(@{$G->{spec_val}}) {
    ...@{$G->{species}} = $G->{dbh}->selectrow_array($G->{sth_species},undef ,$val);
    ...if(@{$G->{species}}) {
    ......$G->{spec_label}{$val} = $G->{species}[0]. '; '. $val;
    ...}
    }

    whether or not this is good code is not the point, I have to make it work, look at all that pointless markup, in python this same thing would look like this:

    for val in G.spec_val:
    ...G.species = G.dbh.selectrow_array(G.sth_species, None, val)
    ...if G.species:
    ......G.spec_label[val] = G.species[0] + ';' + val

    (leading . stands for a space )which version would you rather read?

    or that uselessly verbose java where you have to write X number of lines before any action starts ...

    Python is a simple, clean and powerful language where the real value comes tomorrow or next month, when you have to understand and modify what you wrote today. There are no objective measures of this quality you have to try it to believe it.

  7. Re:Is it just me? by tungwaiyip · · Score: 4, Insightful

    The 'one best way to do it' mostly mean basic language operations, like iterating over a list. Many recipes in the Python cookbook are really mildly complex utility programs. They are differ in features, complexity, performance and intented use. The are naturally many ways to do similar things. And if you take these variables into account, you will find they are not really doing the 'same' thing.

    Since Python 2.x there is a move toward iterators and generators. Rather than seeing this as bloats I would say this Python is maturing to handle real world applications. If you are handling simple data and performance is not an issue, use regular lists. If you writing application to handle arbitrary large data set or performance is an issue, go for iterators and generators. List comprehension is a neat language construct but I don't see any need to rush rewriting anything.