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. "
Let the decorator bashing begin!
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?!
...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.
Alas, there just isn't that much of a reason for me to since most of what I want to do can be done with Bash. This is a reality I don't want to face.
dmiessler.com -- grep understanding knowledge
People say 'Oh, 2.4, Fiiinally, took thier time!'
:-)
Like, hordes of old koreans...
This is more infectious that a mutated H5N1 virus!
#hostfile 0.0.0.0 primidi.com 0.0.0.0 www.primidi.com 0.0.0.0 radio.weblogs.com
and of course I managed to forget (again) that comments, being HTML formatted, ignore whitespace.
Karma: Negative (Mostly affected by dorm trolling)
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.
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.
has anyone written a module that will allow the use of braces rather than whitespace for code-block separation?
If anyone does that, then a lot of the reasons for bitching about python go away. (my reasons at least)
and I am NOT trying to start a python vs <insert language here> war. Just wondering if it's been done yet.
aside from the braces/whitespace issue, the fact that the debugger is an external module and can be problematic, I really like python. (well, it's been a few years since I used it, but it was nice then)
"We are not tolerant people. We prefer drastically effective solutions"
"Significant whitespace sucks? Blasphemy. I add significant whitespace even when it's not neccesary!"
Typing sucks.
This is not about cute. Anyone smart enough, would have python or ruby in their toolbox. Prior to python, I was one of those few who swore by C, I would code anything in C or assembly. C was as high level as I wanted to go, high level langauges for the most part disgusted me. But since I got into python. 99% of my code are python, a few in C. Python is the turbocharger of programming languages.
segmond
Let the bad "In..." joke wars start!
"Your effort to remain what you are is what limits you."
The connotation's wearing my nerves thin
Could it be semantics generating the mess we're in?
I understand that language breeds stereotype
But what's the explanation for the malice, for the spite?
Any chance jython may be updated, soon? It seems it is two versions behind?
Can one tell me why I should learn python and not any other programming language anyway?
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?
Is this going to outdate vPython now? If so, do you think we'll see an update any time soon?
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.
Here's your example in Ruby for instance:
In Korea, only old people use Python.
Only old people ..? :)
Are you talking about north Korea?
I think many open-source developers in south Korea are familiar with Python.
At first I was excited to learn that set was a built in type. But for the life of me I cant figure out what good it is when it only operates on characters. Except for perhaps argument checking, it seems rather useless if you cant operate on sets of words and objects rather than letters.
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.
That entire "debate" consists of one idiot flipping a shit because of a poorly worded snippet from a pre-release version of the "What's New?" overview. He didn't even bother to reference the appropriate PEPs and documentation before grossly misrepresenting the point of the new Decimal type.
No, the Decimal type isn't IEEE 754 standard. No, it doesn't solve, or intend to solve, all the world's numerical analysis problems. Yes, it is a good thing in a language intended to minimize the dissonance between user expectation and actual result.
What the fuck is this supposed to mean?
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
Way to go, and pick a complicated line to use as your example. I think by saying the language reads like pseudocode, they mean:
print 'hello, world'
answer = read_string('what is the answer')
a = str(a)
stuff like that. And don't think that all languages don't have this kind of unreadable code.
Since Python first ran on Windows, it used WinSock 1.1. Starting with Python 2.4, the socket module uses WinSock 2.0.. Most users probably won't notice the difference, but WinSock 2 is highly improved over 1.1 and offers many advanced features such as QoS.
PERL is what happens when you fuse BASIC with a Macro language: maximum flexibility for filesystems. I 3 PERL and use it all of the time for this reason.
Python is the kind of language in which you could develop a small or midrange app without going nuts over mindless "complexity" in the task imposed by the client at the last minute while drinking Starbucks and circling their Jaguar around Central Park waiting for the Opera to start. IMHO.
I would code anything in Python, but I might fix up a library first to give me PERL-like ability on those files, text strings, etc.
The Decimal type! Finaly Python is catching up to COBOL. Yeah.
Actually, its useful. Every language should have it.
I'm not sure what it means, but Python is looking an awful lot like CommonLisp, down to the somewhat controversial syntax.
There are two big differences. One is that CommonLisp made it a lot easier to treat programs as data and vice versa, and it had a built-in high-performance native compiler. On the other hand, Python integrates a lot better with Linux and UNIX, there are tons more libraries for it, and is easier for new users to learn.
Removing all the structure from each of your examples leaves:
For f in glob glob sorted print
Whereas the perl example is
print for each sorted glob
Python introduces unnecessary syntax. Perl lets me get done what i want, in whatever style I want. Python ties you down to it's style.
Perl: Maximum flexibility
Python: Maximum bondage.
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.
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
Perl programmers actually believe any of that any-style-I-like shit is significant? And they wonder why their language is dying...
And your understanding of Perl seems to be a tad limited, since the canonical way to write this in Perl would be:If you are going to badmouth another language, at least have a working knowledge of the language you are advocating (even if the language you are advocating is a piece of shit).
a = [7,8]
b,c = "5" * len(a), ("5 " * len(a))[:-1]
c = c[:-1]
print b, c
I'm positive, don't belive me look at my karma
Divide-and-conquer. Any program is a sum of many smaller bits. The easier the smaller bits...
google for py2exe.
import glob; for f in glob.glob("/tmp/*").sorted(): print f
You're still allowed to use ; to break lines.
I agree with the sentiment, but not the conclusion.
There are still times when we need the low-level, fine granularity of something like C. On those occasions, we also frequently need some high-level features as well, which is where C++ came from.
Unfortunately, the design of C++ has meandered around the design state space so much that many of us consider it to have become quite a mess. Whole *series* of books have been written on the vast number of pitfalls of working in C++.
Dropping back to plain C to get some simplicity and clarity isn't very attractive, for ordinary application work, though. It lacks too many useful things.
Instead, we are told to "use a subset of C++" in some special way that changes over time. That's supposed to bring back the simplicity without sacrificing power. So now every developer uses a different subset a different way, meaning all "good C++ developers" need to have an encyclopedic knowledge of arcane trivia to work with other developers. Where did the simplicity go?
I think the best approach is a language that is as low level and simple as C, but has most of the most popular high-level features of C++ and even many Python-like conveniences, and that's the "D" programming language.
It's still in beta, but it has progressed far enough and is out in the open enough that it looks as though it will become a reality, unlike Paul Graham's Arc.
Sticking as close to C as they have means that it should be relatively easy to slip it into the massive existing infrastructure of libraries and OS calls that exist for C and C++.
It's like C with many of the conveniences of Python built-in but overridable.
It's not as easy to work in as Python, but when you need to use either C or C++, you might be able to get away with using D. Despite the many Pythonesque features, it's still so close to C that a D compiler can statically link compiled D with compiled C into a standalone executable.
I hope to be able to switch from my current "subset of C++" to using D in the not too distant future.
"Those who have never entered upon scientific pursuits know not a tithe of the poetry by which they are surrounded."
- called Wumpus elsewhere.
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.
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.
You're quite right - functions in Python are first-class objects.
So are classes, bound and unbound methods, and everything else.
It's possible to do some serious magic with metaclasses, making use of the fact that classes are just objects. Stuff like that can hurt to look at, though, where 'a' is an instance of 'A', and 'b' is an instance of 'a'. *winces and rubs brain*.
The issue with 'float' is actually a hardware limitation. It's present in all languages that use the floating point hardware. Python, however, doesn't hide it from you when you print the floats - AFAIK that's pretty much the only difference.
What it boils down to is that floats are a PITA. That said, in practice I've had few issues, I just remember the usual rules like never comparing floats for equality, only by a range, and always truncating them when printing. I do exactly the same thing in C++.
I'm doing it now, and I must agree - it's pretty slow going.
... *sigh*. Still, most of the low-level API is pretty good once you know it - it could just use a big improvement in the documentation.
It's fairly fast and simple to implement a module and a bunch of functions, I'd find it hard to complain about that.
Unfortunately, the same cannot be said of building objects, which I find a considerably more irritating processes.
There are a bunch of tools that others have mentioned to help out, of course, but they don't fit every situation. I'm just having to code the lot by hand
What _is_ endlessly frustrating is the lack of a standard C++/Python interface shipping with Python. It really sucks to have to implement Python objects as plain C functions in C++.
I'm with you on the functional style issue.
I always hated functional programming, having been forced to learn to use utterly crippled pure-functional teaching languages. Uggh. Consequently, I thought functional programming was pointless academic wank.
I didn't forget how to do it, though.
With the Python 'map', 'filter', 'zip' and 'reduce' functions, I found myself breaking into segments of functional style again, where it was faster / simpler / safer / cleaner.
List comprehensions increased this further, largely by eliminating the horror that is the nested map and filter jungle.
Some programs, especially data processing ones, I now write almost purely functionally. I'll usually write a series of functional blocks that perform each major transformation, put them in separate one-line functions, then perform each block in series in the main() function.
I generally find this results in much less buggy, faster, and better designed code.
The most important thing is that I can use imperative code where it's more appropraite, and functional code where it fits best. That's just fantastic.
Perl: Maximum flexibility Python: Maximum bondage.
Yes, that is why you have a choice. Now if you have to maintain code written by other people, you'll appreciate Python's forced structure. I've been there and done that. And how many times have I wanted to whack some Perl hacker on the head who only thought of himself when he was writting his code.
What issue do you find? Java also have the same design with a float data type and a BigDecimal class for arbitrary-precision decimal numbers. If you are complaining that C doesn't have a Decimal type that's more understandable.
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.
Normallly yes, but using them between an "import" and a "for" triggers a syntax error. Try it yourself and see:
Ita erat quando hic adveni.
Python 2.3.3
Then I stand corrected. :)
But I like Lua better for embedding into apps. And as a lnaguage as well. Pygame is horrible and slow IMHO.
click me
Matlab, famously, has an array type (the basic data type is a 2-D array or matrix -- hence "Matlab"). While Matlab is interpreted and slow, functions that operate wholesale on array types are fast. Java has an array type, and they seem to have done a good job of it with efficient bounds checking. At one level, a Java array definition is a class like any other class, but an array is special case (you cannot "extends" an array type) that gives the required efficiency.
While the early Lisps didn't have arrays, I am told Common Lisp and the other dialects stemming from the Lisp machine days where the emphasis was on having an industrial-strength language, they all had arrays, and they even had neat ways of taking array "slices" -- passing a subrange of an array and having bounds checking apply to that subrange.
Python is working on arrays -- Numeric and Numarray -- but adoption seems slow in stuff like plot and graphics packages.
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 am looking for a good plot/graphics package for Python that supports on of the Python array types to make this transition.
way to make a point, unfortunately i believe you made the other guy's point.
What's so unreadable about that? Once you get used to the idiom of "shift in a sub gives you the next argument" you'll be set for life.
all of it, you just admitted you needed to know something about the code/language to understand what it was that you wrote. anybody should be able to understand what the block of Python code did without having ever seen Python before (but obviously _some_ programming experience is necessary) because it reads as psuedo-code.
Please don't read my sig.
I didn't see this in the discussion yet. The number one reason for me to upgrade to 2.4 is that it's the first (official?) release to be built with MSVC 7 (for Windows, of course). As of 2.3, the distutils package would barf if you tried to build extensions with MSVC 7 instead of 6. Apparently MS changes enough stuff between major compiler releases that lots of little bugs would pop up for extensions built with a different version.
...), I
tried rebuilding Python 2.3.4 and various
libraries (numeric, etc) with MSVC 7. This
was a huge pain particularly when I found a
new library. I wanted to use the 2.4 betas but the boss wouldn't go for it. I was forced to live with MSVC 6. Feel my pain, people.
As MSVC 6 is so truly horrible for writing anything advanced (or using Boost
18 months and they hit the release target almost bang on. Way to go!
Assembly maps one-to-one to machine code while C doesn't map one-to-one to machine code (particularly when high levels of optimization are involved).
>>> import os; print [i for i in range(0,10)]
How's that ?
No No, don't make a new keyword. Just make it an attribute:
myset.immutable = True
It may not be practical, however, to undo a change into immutability. Once immutable, always immutable.
Try it. If nothing else the experience will make you a better perl programmer. I found the same when I tried perl. Personally, I prefer python for a simple reason: code intelligibility. You can go back to python programs months later and understand them immediately. The absence of braces is helpful too, means you don't have to move off the home keys when coding, and it's one less thing to type, but unlike many perl abbreviations you actually improve readability by removing them - the eye follows the indentation, not the braces, so it's better when the interpreter does too.
I am trolling
Yeah, I'd love to use a language whose maintainer doesn't seem to understand the importance of Unicode. 'Cause ASCII and EUC-JP is all we need, right? :rolleyes:
I'll match my "Magnificent 7" against your measely 2.4 any day. Finally something to feel good about.
2.4...Bahhhh!
I was just being a smartass.
click me
for f in (glob.glob("/tmp/*")).sorted() : print f
AttributeError: 'list' object has no attribute 'sorted'
The OP must have typed that wrong. It should be:
for f in sorted(glob.glob("/tmp/*")): print f
print "$_\n" foreach (sort glob "/tmp/*")
I dare python lovers to write something like that in python in one line.
Why is one line better?
But anyway (assuming you've imported glob already--you'd need to do that for your Perl, too):
for f in glob("/tmp/*").sorted(): print f
(print already newline-terminates, but you could easily "print f+'\n'" or whatever)
rage, rage against the dying of the light
First, if you've already done the right import then its:
for file in sorted(glob("/tmp/*")): print file
which comes to:
for file in sorted glob print file
which is much easier for an English speaker to understand than the perl, IMO:
print for each sorted glob
second, I'm not really sure how else you'd want to express this. But if you're building a list for more complicated manipulations, Python provides many styles to do it.
rage, rage against the dying of the light