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. "

12 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:genexps by CoughDropAddict · · Score: 3, Insightful
      Oops, I realized that probably the best way to do this in ruby is to use inject:
      print objGenerator.inject(0) { |sum, value| sum + value }
      No intermediate list, and you don't have to rely on having a function like "sum" defined -- it could be any operation.

      You could also add this to the "Enumerable" mixin:
      module Enumerable
      def sum
      return inject(0) { |sum, value| sum + value }
      end
      end
      Now you can call "sum" on any object that implements "Enumerable", and the example becomes:
      print objGenerator.sum
  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:I *want* to be enthused, but... by Anonymous Coward · · Score: 3, Insightful

    Here's a novel idea: Use the right tool for the job. There's a very large class of problems for which neither c nor bash are particularly appropriate programming languages. I suspect that Linus was attempting to comment on the partcular classes of problems on which he typically works, not on the merits of any specific programming language. If everything you're doing now can be done with bash, then use bash. However, don't rule out Python just because Linus doesn't use it. One day, you'll need to do something that's impractical with bash, and you'll find Python quite useful.

    I use Python extensively in my day-to-day work. It's the most versatile tool in my toolbox, and I'm continually amazed that so few people, at least around here, use it.

  4. 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)
  5. 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.

  6. 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.

  7. sigils sneaking in by CatGrep · · Score: 3, Insightful

    So now that Python is using the '@' character for function decorators the Pythonistas can no longer crticize other languages like Ruby (which uses sigils responsibly) for their use of '@' (I'm not including Perl in the list of languages that use sigils responsibly, btw :)

    Welcome to the sigil club! Pandoras box is now open! '$' can't be too far behind :)

  8. WTF? by ArbitraryConstant · · Score: 3, Insightful

    Linus is not some kind of deity. He (by his own account) does nothing between kernelspace and small scripts.

    I'm very proficient with C, Python and Bash, and I can tell you for a fact: Bash and Python barely compete against each other.

    Bash has nothing in the way of nice datatypes. Bash is very slow while Python can be nearly as fast as C (I've gotten it up to 80% when relying heavily on C libraries like regex). Bash can't do more than the most trivial things without helper programs, which while useful, takes forever because it has to keep spawning processes.

    For a high level language, Bash has pathetic memory management. Pretty much the only way to get some things done is tempfiles, which is worse than malloc because they're not removed if you don't clean them up.

    If you can't hold more than two languages in your head, go home and learn Java and C#. You're only going to get made fun of on slashdot.

    --
    I rarely criticize things I don't care about.
  9. 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.

  10. 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.