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. "
It took them a while, but it's worth it. Here's some of the new features:
/usr/lib/python2.4/pdb.py
* 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
Finally I can make my functions so much prettier.
I don't need a signature.
If you can keep your "significant whitespace sucks" comments as children of this one, I'm sure everyone will get along just fine...
import overlords
print "I for one welcome our new %s overloads.!" % overloads.get_random()
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.
cue grumbling about how Christmas is overcommericalized+etc
The World Wide Web is dying. Soon, we shall have only the Internet.
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
2,500KVA @ 0.8 PF? (note: some electrical background required to appreciate the above)
Karma: Negative (Mostly affected by dorm trolling)
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.
Instead of having a special keyword for immutable sets (frozenset) wouldn't it be better to have an "immutable", "final" or "const" keyword?!
Python is a pretty good language for general use and for teaching. If fact, what got me interested in Python was a story (on Slashdot) about programmers at Sun saying that Python was a better language than Java in their environment.
Having said the above, there always seem to be 'issues'. Floating point numbers are one such issue. This release fixes that with 'Decimal'. The trouble is, you have to know about 'Decimal' before you can use it. This raises the difficulty of using Python.
I find that I write code quickly and then spend an hour researching some module or other. It sure slows down the process.
What were Linus' comments?
I do not see "Extensions API" anywhere on the list of improvements. Last I tried, creating one's own object type or even a simple command was a rather tedious task, unlike in TCL...
In Soviet Washington the swamp drains you.
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.
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
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.
and of course I managed to forget (again) that comments, being HTML formatted, ignore whitespace
;)
Can't say the same thing about Python.
python -c 'print "55 5 5";'
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"
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.
Grrr, the @ sign is the Ruby way to declare an instance member. This is a declaration of war I say!!! (next thing he'll do is to drop the indentation and add end at the end of all the blocks ;)
There are GUI toolkits which typically are added on as an extension to something like Python. I am learning Ruby and can choose from Qt, Tk, Wx, Fox, etc. As long as someone has created the extensions you are good to go.
Now we just need to wait for 2.4 versions of py2exe, cx_Freeze3, wxPython 2.5.3.1, SPE, IDLE, PyQt, SWIG etc. before we can start using this!
I expect some software will even die now - like McMillan Installer, as development stopped on 2.3, unless 2.4-devel has some excellent backwards compatibility with 2.3 libraries.
I'm sticking to 2.3.4 for a good while yet, don't have a use for decorators.....
#include <sig.h>
I've always liked Python, but I don't think this update is enough to make me learn it.
In one respect, it is exactly what I've been hoping for. No more sweeping changes or vast syntactic variances, but they have eliminated some usability problems and silly errors. It's a very mature language now, and seems to be behaving as such, this makes me happy
Still, though, they seem to be competing for a niche that Perl has a deathgrip on for me. I use Bash whenever I can, Perl when I can't or it would be ugly, and C when I feel like I haven't had my eyeballs gouged out with hot pokers enough lately -- er, I mean when performance is at a premium.
Python's capabilities seem to rapidly be approaching what I can do with C, and God knows I want to never malloc() again, but as long as compiled binaries can be made only "Not easily," I don't think it's going to unseat Perl for my heavy-duty scripting language slot.
adam b.
Does it use that new Parot thingie that Python and Perl were supposed to share?
Ita erat quando hic adveni.
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.
The Decimal type! Finaly Python is catching up to COBOL. Yeah.
Actually, its useful. Every language should have it.
Ita erat quando hic adveni.
(Warning: Flamebait lookalike ahead).
:)
IMHO - there are still uses for C, there are absolutely no uses for C++ nowadays. Moreover - there is no C++. I mean - I've been tracking (and sometimes using) C++ developments since the early days till recent and I can tell you - the amount of pileup that they produced in modern C++ compared even to early C++'s (not to say about more sane languages) is totally ridiculous. No - inventing a new language construct for bloody every particular problem does not make a good language. Well - if you want a complex system - use dynamic languages (Python, Lisp, Smalltalk - whatever suits you. Heck - PHP even if you know how to manage your globals). If you want something small and embedded - you should not overcomplicate - use C (refer to Linux sources, for example, to see how people code in C). If you want low-level performance and reliability in complex systems - I hate to say it, but use Java (large portions of IL2 - flight simulator - are written in Java, totally amazing stuff). Of course, if you like brainfucking - C++ may be for you, but it's really a bird that does not fly. On amount of time and energy that you'll waste on learning C++ you can pick half a dozen of better languages.
If still not convinced - check out how many CPU instructions are wasted for such a simple thing as returning a C++ string object from a function. When I show it to C++ "gurus" many are enlightened.
Don't worry about it. It's still a good book and very little (if anything) has changed that affects the book's accuracy. It won't tell you about new features of course, but those are all either dead easy to learn or not likely to matter to the average new pythonista.
Currently in Python, the C floating point libraries used will produce this:
>>> 1.1
1.1000000000000001
Thus, the Decimal data type was born.
From PEP 327: "The inaccuracy isn't always visible when you print the number because the FP-to-decimal-string conversion is provided by the C library, and most C libraries try to produce sensible output. Even if it's not displayed, however, the inaccuracy is still there and subsequent operations can magnify the error."
Function Decorator Proposal/Specification
Its nice to see a language evolve in favor of use without sacrificing readability and overall utility.
Before:After: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.
I refuse to use any language that would take that as code rather than noise.
no taxation without representation!
But seriously, Common Lisp is the Common Lisp for the 21st century. It's doing quite nicely; in the past couple of years folks have taken this "old" programming language and added excellent support for all kinds of current needs -- a Web application server (TBNL), a remarkable SQL interface (CL-SQL), general-purpose dataflow programming (Kenny Tilton's Cells), PDF typesetting and generation (CL-PDF, a rival to TeX!), OpenGL ....
(Peter Seibel's Practical Common Lisp is highly recommended for the "modern" programmer interested in this "old" language. Practical examples include spam filtering, an MP3 database, and a Shoutcast server.)
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
If all the C programmers vanish, what are we going to write the next generation of high-level languages in?
That's a very good question. However, just because programming in C is difficult doesn't mean that it's the best language to write high-level languages in. C has many design flaws that people have gotten so used to, they assume there's no other way of doing things.
If you like programming in C because it's a challenge, then by all means do so. However, I would classify that hobby with people who program in Intercal and similar. Those languages are challenging too.
I see a real need for a language that is as easy to program in as Python, yet as potentially fast as C. As far as I know such a language doesn't exist yet, but I see no fundamental obstacles preventing us from creating one. It's just a matter of time.
Do you really want a future where not a single person actually knows what is happening inside that CPU, just because "it's hard?"
Yes, an essential part of a programmer's training is learning exactly what is happening inside the machine. However, a human being cannot keep track of everything at once. That's why we use abstraction. A high-level language (not C) abstracts away the details the computer is smart enough to handle, and leaves the material which the computer is not smart enough to handle.
Programming is fundamentally hard. Making the job harder than it needs to be can be fun, but for day-to-day work, it just doesn't make sense.
TTFN
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.
nice try.
C is the defacto for embedded systems. it's smaller and faster and there are libs available that make your app 1/10th the size of a regular C app.
ther are no embedded libs for C++ that are solidly tested and compatable with the regular libs.
Embedded programming is getting huge, and C is a part of that.
Do not look at laser with remaining good eye.
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: 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: 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.
Can one tell me why I should learn python and not any other programming language anyway? ...uh, because your brain is actually capable of learning more than one way to do things. Some people even know up to *three* programming languages!
:)
Sarcasm aside, it's not uncommon for me to work in four vastly different programming languages in a single day (usually java, ocaml, python, and C). That's not counting the ``lesser'' languages like sh, javascript, etc...
If I hear people talking about how a language or development environment does wonders for their productivity, I'll give it a shot. I can usually pick up languages pretty fast. I admit it took me a week or maybe even longer before I really ``got'' ocaml, though. But I had a good time trying.
I learned python initially because it had the best xmlrpc implementation. My first actual project was one that read a format definition for the tiger database and then used it to parse all of the zip file exports to load them into postgres. I ran it on several machines for about a week with very few errors (my own errors, that is) after spending a couple of hours writing the loader. That impressed me quite a bit for my first use of a language.
Don't look for an excuse to learn something, though. Just do it. Give it some time. If you don't like where it's taking you, then take what you got out of it and learn something else.
If all you know is VB, though, it's going to take quite a bit of learning before you know what a good language feels like.
-- The world is watching America, and America is watching TV.
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.
I think he meant *through* a GUI, not "make a GUI".
There's plenty of python "dev studio" things. The latest kdevelop supports it natively, and there's also 'idle'. thekompany produces 'blackadder' too.
There's loads of other half-arsed attempts at creating a nice gui, and probably some full-arsed ones that I've not thought of.
...I am inclined to get more profficient with Bash and C...
But Bash does not do anything. To accomplish something you have to use all the command line tools, like grep, sed, and awk. Put those tools in the language and you have Ruby or Perl.
Shell scripts are only really quick to develop because they mirror what we would type via the CLI. Thus the name, "script".
All interpreted languages are abstractions over Lisp
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:
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:
(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.
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.
You example is a very powerful one liner!
You need to learn a few Python idioms. Anyone who work with Python for a while should have no problem understanding this.
1. x and y or z is Python's version of ternary operator. If is similar to x ? y : z in C.
2. " ".join(list) is similar to string.join(" ", list). I won't go into detail here but your expresion collapse a string with multiple whitespaces into single space.
3. The lambda construct is a function factory. Thing of callback in C but Python allow for much more elegant functional style programming.
The entire expression create a function which can collapse or not collapse a string base on you choice. The best thing is to see it in action with two more lines:
for line in file('xyz.txt'):
print processFunc(line)
This will output a files with whitespace collapsed or unmodified depends on the collapse flag.
Every language has its syntax and idiom to learn. Is the syntax below readable? Yes if you actual know the language.
for (i=0; in; i++) {
If you expect 0 learning and that each line of Python maps to something you are familiar with such as C, that's not what Python really is.
If you're going to nitpick about how man CPU instructions it takes to return a std::string, you better not use any high-level language.
Take a look at the job market, good knowledge of C++ is still an invaluable skill.
Moreover take a look at libraries like boost or systems built on C++ like KDE. Well written C++ can be just as elegant as any language (it's always in the programmers hands, they can turn anything in to spaghetti). There's a reason why it's been so popular and many of its concepts are used in other languages (not to say that it was original in its ideas, but in its use of them).
Any serious numerical programming needs an array type. List types based on indirection, whether from Lisp or Python just don't do the job -- too much overhead.
Python "lists" are extensible 1D arrays; there is no extra indirection.
Python is working on arrays -- Numeric and Numarray -- but adoption seems slow in stuff like plot and graphics packages.
Numeric and Numarray are widely used by people who need them and they are great and mature packages. But I think the fact that they aren't used universally is deliberate: if their performance and/or functionality isn't needed for a particular application, it's probably better to stick with regular Python lists to keep things simple.
I would like to use Python as a replacement for Matlab -- I am bothered that we are so stuck on Matlab in our engineering curriculum -- a university shouldn't be shilling for a properietary language from a single vendor. Besides, Matlab is so ad hoc and cobbled together -- I think we could use something with a better theoretical foundation in language design.
I fully agree there. And the way to make that happen is to create more packages. I am writing numerical Python packages and making them available publicly. The more people do that, the sooner we can replace Matlab.