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

29 of 359 comments (clear)

  1. 18 Months by nijk · · Score: 4, Informative

    It took them a while, but it's worth it. Here's some of the new features:

    * multi-line imports - when using imports in the form from foo import bar, baz, bing, bang, you can surround the imported names with brackets, and they can be split across lines. This is part of PEP 328.
    * Farewell to OverflowWarning - as documented in PEP 237, Python no longer generates OverflowWarnings.
    * function/method decorators - function and method decorators, first described in PEP 318, have been added to the language, using 'pie-decorator' syntax. Decorators are on the line before the 'def', and prefixed with an '@' sign. (PEP 318)
    * Assigning to None - the compiler now treats assigning to None as a SyntaxError.
    * Failed import cleanup - when a module import failed, versions of Python prior to 2.4a2 would leave a broken module in sys.modules - subsequent attempts to import the failing module would silently succeed, but use the broken module object. The import machinery now removes the failing module from sys.modules if the import fails.
    * The -m command line option - python -m modulename will find a module in the standard library, and invoke it. For example, python -m pdb is equivalent to python -m /usr/lib/python2.4/pdb.py

    1. 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)
    2. Re:18 Months by ReelOddeeo · · Score: 4, Funny

      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.

      My functions do each fit on one screen.

      For any sufficiently large definition of screen.

      --

      Those who would give up liberty in exchange for security and DRM should switch to Microsoft Palladium!
  2. function decorators by koi88 · · Score: 4, Funny


    Finally I can make my functions so much prettier.

    --

    I don't need a signature.
  3. heh by boschmorden · · Score: 5, Funny

    import overlords

    print "I for one welcome our new %s overloads.!" % overloads.get_random()

  4. department of redundancy dept. by Anonymous Coward · · Score: 4, Funny

    The python final release of python 2.4 is finally released for python users out there who wish to use the final release of python release final.

  5. 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.
  6. On Decimal and floating point by Frater+219 · · Score: 4, Interesting
    The What's New document gives a somewhat inaccurate description of the importance of the Decimal type. Naturally, switching from binary to decimal changes which fractions can be exactly represented, but it does not change the fact that some cannot.

    The importance of decimal arithmetic is not that it is more accurate than binary, but rather that it conforms more closely to what people expect from using decimal in daily life. These expectations are codified into various social rules such as accounting practices.

    There's been some heated discussion of Python 2.4's Decimal, in this very regard, on the Lambda the Ultimate languages blog.

    1. Re:On Decimal and floating point by Frequency+Domain · · Score: 5, Funny
      So, obviously, we should be using base (apply '* *n-first-primes*). How about 30 or 210? At least it's not an arbitrary base.
      It doesn't matter which base you use. All your base are belong to us.
  7. frozenset by hey · · Score: 4, Interesting

    Instead of having a special keyword for immutable sets (frozenset) wouldn't it be better to have an "immutable", "final" or "const" keyword?!

  8. Dive into Python by cbelle13013 · · Score: 4, Interesting

    I had absolutely no interest in Python until I read Slashdots review of Dive Into Python. Its right on target and got me excited to run home and see what I could do. Of course that only lasted for about a month, but I'll be sure to head home and take a peak at it again.

  9. Re:I *want* to be enthused, but... by ultrabot · · Score: 5, Interesting

    after Linus's comments I am inclined to get more profficient with Bash and C and almost ignore Python completely. It's so dissapointing though - I really wanted to learn Python; it's such a neat language.

    Linus explicitly mentioned that he doesn't do anything "in the middle" - it's either kernel hacking or something trivial enough to do with bash. Just go ahead and learn Python - you will find that it's *easier* than bash, especially if your programs might have errors (which they do).

    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.

    Go read ESR's "The Art of Unix Programming", available online for free.

    --
    Save your wrists today - switch to Dvorak
  10. Is it just me? by digitaltraveller · · Score: 4, Interesting

    Or has python strayed from it's original philosophy of 'one best way to do it'?

    I used python in the 1.5 days. The syntax was incredibly clean. Nowadays the language has tremendous idiomatic power, any programming language researcher should be familiar with it.

    But that power has brough alot of complexity. At the end of the day, languages are tools and the learning curve to understand (particularly others) python code seems to be increasing.

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

    2. Re:Is it just me? by Jerf · · Score: 5, Informative
      Or has python strayed from it's original philosophy of 'one best way to do it'?

      To a degree, yes. Largely to the extent it has it is a result of backwards-compatibility; while the Python designers do not make the mistake of enshrining reverse compatibility above all else, they do try to avoid gratuitously compromising it.

      As a result, as better ways to do things have emerged, sometime the old ways hang around and muss things up. However, for any given version there is always a "core" that you can stick to that is very close to "one best way to do it"... and despite what a first reading tells you, you really don't throw much of the language away.

      For instance, while in modern Python you can say either
      a = []
      for i in range(5):
      a.append(i*2)
      or
      a = [i*2 for i in range(5)]
      the latter is the "one right way to do it". Few, if any, new additions truly leave you with two equally good choices.

      (One of Guido's examples is the "lambda" statement, but a lot of people, including me, rather like not needing to replace
      self.register("event", lambda event: self.keyPressed())
      with
      def handler(event):
      self.keyPressed()
      self.register("event", handler)
      but hey, that's life. (While that isn't code for any GUI toolkit in particular that is a pattern common to all of them.))

      By and large, none of these things have affected the difficulty of learning Python from scratch and using its libraries. It has affected the difficulty of reading other people's code, but I find the alternative, "keeping the language stagnant indefinately", completely unacceptable, and frankly, reading code is hard anyhow. (I was writing code for others long before I was reading other's code.)

      In fact, given as that is the alternative, "keeping the language stagnant indefinately", while I concede it is somewhat sad that we can't jump to an optimal language immediately so that nobody ever has to learn anything past their first impression (not sarcastic, that would be the ideal), that doesn't seem to be working for folks. You might try LISP, though, I gather that hasn't changed syntax, much. (Though I also gather mature programs in that language tend to start looking like their own languages themselves, so that just may move the pain...)
    3. 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.

  11. Re:Pah by Anonymous Coward · · Score: 5, Funny

    python -c 'print "55 5 5";'

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

  13. Re:Python is a pathetic language. by tuffy · · Score: 5, Informative
    I dare python lovers to write something like that in python in one line.

    for f in glob.glob("/tmp/*").sorted(): print f
    Though you will need to import glob in a seperate line. But I fail to see how doing trivial things trivially is a helpful measure of a language's usefulness.
    --

    Ita erat quando hic adveni.

  14. Re:Writing extensions... by imroy · · Score: 4, Informative

    The Boost project has Boost.Python. I haven't used it yet, but the docs make it sound very interesting. It looks quite simple to write base classes in C++ and then subclass them in Python.

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

  16. Re:Sets in Python by tuffy · · Score: 4, Informative
    Sets can operate on anything. But you need to pass in a list of those things to build a set out of, such as:
    >>> s = sets.Set(["foo","bar"])
    >>> s
    Set(['foo', 'bar'])
    >>> 'foo' in s
    True
    --

    Ita erat quando hic adveni.

  17. Not a Python Programmer... by pragma_x · · Score: 4, Interesting
    ...but I am impressed with the whole funciton-decorator addition to the language.

    Function Decorator Proposal/Specification

    Its nice to see a language evolve in favor of use without sacrificing readability and overall utility.

    Before:
    def foo(cls):
    pass
    foo = synchronized(lock)(foo)
    foo = classmethod(foo)
    After:
    @classmethod
    @synchronized(lock)
    def foo(cls):
    pass
    Readable is of course in the eye of the beholder... and I am a C and Java programmer with a sizable fetish for D. So while I don't find the syntax all that pleasing to me in terms of my own work, it certainly changes things for reading python code.

    I was also stunned to learn how flexible decorators were in the previous version of python. It's refreshing to be see functions treated as objects... unless I'm mistaken about the concept.

    However its a huge shame that the new decorator syntax isn't supported for classes in 2.4. Seems like that's going to become a wart on a rather consistent language syntax, IMO.

  18. Re:OK Trolls... by pclminion · · Score: 4, Informative
    Do you really want to go through each line, scrolling all the way to the end because your language has significant whitespace?

    There is absolutely nothing that says you can't break things across lines. In most cases you don't even need a backslash to escape the linebreak. TRY the damn thing before criticizing it.

  19. Re:Writing extensions... by William+Stein · · Score: 4, Informative

    One can write extensions in Pyrex, which is a language that is very similar to Python. Pyrex code is converted to C, which is ccompiled into an extension to Python. One can also access any C libraries from Pyrex.

  20. Re:CommonLisp for the 21st century?! by geg81 · · Score: 5, Informative
    Python looks NOTHING like common lisp.

    Python is a lot like CommonLisp: dynamic typing, reflection, eval, lexical scoping, extensive iteration and looping constructs, strings-as-sequences, and on and on.

    For once thing is the "big difference" you describe: you can't transparently process code as data. That means no MACROS, which is what makes Lisp so damn powerful.

    Go back and read the original papers on hygienic macros: you don't need Lisp syntax or code-as-data in order to have a macro facility as powerful as Scheme's (and Scheme got by for many years without macros anyway). I wouldn't be surprised if Python at some point gets a hygienic macro facility. Furthermore, there is a separate data syntax for Python that takes source that looks very similar to Python code and represents it as a DOM tree.

    How do you return an anonymous function from a function in Python?

    In the obvious way:
    A ``def'' form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def.
    That part is actually more natural than doing the same in CommonLisp (Python, like Scheme, but unlike CommonLisp, does not have separate value and function slots on symbols).

    How do you build a function at run time? It's not easy or obvious.

    There are two things you could mean by that. The first is to build it from source or structure. You can do this:
    exec "def c(x): return x*x*x"
    The second is to build complex functions using functions that take functions as arguments. You do that the same way you do in CommonLisp, since Python supports the same primitives: lexical closures, dynamic typing, and functions that take functions as arguments.

    I think people see "lambda" and they somehow think that Python has something to do with Lisp.

    I think Lisp zealots incorrectly think nothing that isn't exactly Lisp could even come close. Python, in fact, is very close to Lisp; the two big differences are syntax and lack of macros. Lack of macros can be addressed, and there are separate Python-like syntactic representations of data.

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

  22. Re:Supprised by Junks+Jerzey · · Score: 4, Informative

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

    I expect this isn't the answer that true Python devotees would express, but here goes anyway: It's a very high-level, very dynamic, language that's reliably cross-platform.

    "Very high-level" differentiates it from Java, which I see as more mid-level. It's also different than Perl 5, which is higher-level than Python in some ways, but convoluted and crusty in other ways (anything involving nested data structures, for example).

    "Dynamic" means you can test code interactively, you don't a build process, you don't waste time enumerating things and creating redundant headers and so on.

    "Reliably cross-platform" is the key. This is where Scheme and Lisp and Haskell fall down. Lisp has a standard definition, but the community is fractured by there not being a standard implementation. You can argue that diversity is good and all that, but it does tend to hurt overall. Python has a huge number of standard library modules as a result.