Learning Python, 2nd Edition
Python is a dynamic, interpreted, object oriented language used for both scripting and systems programming. Python is known for being easy to learn and use, while also being powerful enough to be used for such projects as Zope and the Chandler project. Its growing popularity is also based on its reputation for fostering programmer productivity and program maintainability. One drawback sometime cited is its relatively slow execution speed compared to compiled languages such as C.
For myself, I have probably read too many books about Python, but that is because I am an amateur hacker who learns programming slowly, and I find that reading several books about the same topic, covering the subject matter from different angles, allows me to better absorb the material. For me, this was a good review of the core language and a welcome refresher course on the newer aspects introduced in versions 2.2 and 2.3. For anyone who is new to Python and wants to learn from the ground up, this book would be a great place to start.
Mark Lutz is an authority on Python and one if its leading teachers, with both Learning and O'Reilly's Programming Python to his credit, as well as the courses and seminars he teaches professionally. In updating the original version, which was already very good, Mark has polished the chapters on the core language to a nearly perfect level, while his co-author David Ascher has done the same on the more advanced aspects of the book. In addition, Mr Lutz has benefited from extensive feedback from students and readers, and his explanations therefore anticipate common misunderstandings. Each chapter is accompanied by a problem and exercise section and answers are included at the back of the book.
A major addition to the new edition is a chapter on "Advanced Function Topics," including list comprehensions, generators and iterators. Python is sometimes used with a functional programing style almost similar to Lisp, although to List purists that may sound like heresy. The recent versions of the language have significantly upgraded Python's support for the functional style. Functions cover three chapters in the 2nd edition instead of just one.
Another major change since the first edition is extended coverage of Modules, which now occupies four chapter instead of just one. Python modules are a high level package structure for code and data, and they help facilitate code reuse. Yet another addition is coverage of Python's "new style classes." Coverage of classes and object oriented programming has been greatly expanded and now includes five whole chapters and almost 100 pages. Coverage of exceptions now is expanded to three chapters.
If you have been considering learning Python, now would be a great time since this new book is the perfect introductory text. If you already know Python and have read the first edition of Learning Python or another introductory text, then this book may not be essential since the new language features are covered pretty well on the web in various places, and you might be better advised to read one of the other fine books on non-introductory aspects of Python. But this book is about as good an introduction to the language as you are likely to find. The book does not cover all of the Python libraries nor many other topics, but it does briefly touch on the major libraries, frameworks, gui toolkits, and community resources.
If you want to learn the core Python language quickly, this may be your best bet. Learning Python only covers the basics, but it is deep in information on what it does cover. Well written, understandable, and in a very logical arrangement, this book is densely packed with info.
I have often found myself returning to the original book, and the new book will now fill this role. It is deep in information, well written, and a joy to read. For an experienced programmer who is just learning Python, it may be possible to thoroughly learn everything about the core language in one reading of this book. For relative newbies, it will be an often-used resource.
To read more reviews of books about Python, visit the Python Learning Foundation. You can purchase the Learning Python, 2nd Ed. from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
I have nothing against python, but every time you ask a python nut why they choose to code in this language, the response is always something to the effect of "it's easier than perl". I don't mean to troll here, but does python have any merit of its own, or do people only use it because they don't want to use perl?
"Quoting famous computer scientists out of context is the root of all evil (or at least most of it) in programming." - K
While TCL remains my personal favorite, Python is really good, except for the creating-your-own-extensions part. The Python's C API needs a lot of catching up to do to match the excellence of TCL's.
In Soviet Washington the swamp drains you.
Well, from the perspective of a long-time Perl developer, Python has a certain elegance in it's language design (apart from using whitespace as syntax ;) that some other languages lack (in fact, it reminds me of Smalltalk in many ways, for this very reason). It has taken many of the features from various object oriented, functional languages, and scripting languages, and tied it up in a nice, clean, consistent little package.
OTOH, Perl as a language is unbelievably flexible and convenient to work with, but it's most definitely a more "hackish" language, in that it's grown more than it's been designed. As such, it's definitely more of a developer's language (ie, has many of the features which, while not necessarily incredibly elegant, are *really* convenient) than a theorist's language.
So, then, why pick one over the other? Frankly, in the end, I suspect it's just personal preference (or predjudice).
The importance and utility of this can't be overstated. Python absolutely rocks as a rapid development environment. I have not personally experienced a language that lets me go from concept to implementation nearly so quickly. Once an application is up and running, Python provides a great toolset for profiling your project and making it easy to replace performance-critical sections with the low-level language of your choice.
Does your crypto application need a faster random generator? Replace parts of that module with C. The rest of your project still gets the benefits of a strongly typed, object-oriented language with a robust library of string manipulation, pattern matching, and GUI interfacing functions.
It really is a project manager's dream come true. Python has replaced Perl as my language of choice for all new development.
Dewey, what part of this looks like authorities should be involved?
It's too easy to accidentally use do-while when you should have just used while. It actually makes the language less error-prone, because in those few cases where you do have an unconditional first pass, you are forced to structure the code differently and actually think about what you're doing.
You can always transform: do stmt while foo into stmt while foo stmt which isn't even longer (if stmt is actually many statements, it should be a function anyway). It's not worth introducing an abusable language construct just so you get to be lazy and not code a function when you should.
Boost.Python
I sort of hope that Parrot will help Perl overcome its introversion, and let it integrate more readily with other languages.
I think that C++ and Python form a dynamic duo. You can put the effort into compiling items that benefit from such, and glue them together in Python most agreeably.
The C++ standard library focused on Platonic abstractions, but Boost is pulling C++ in more mainstream directions. And that's a beautiful thing.
While issuing random plugs, check out Leo. It's not too often you see a fresh interface on an editor.
Get thee glass eyes, and, like a scurvy politician, seem to see things thou dost not.--King Lear
I guess Perl is just traditionally what you do these things with. It's not necessarily better. Perl also doesn't support Windows directly like Python does - if you want Perl in Win32 you pretty much have to go with ActiveState whereas Python.org has a Win32 specific distribution. Then again, it's difficult to compete against CPAN's sheer size.
But anyway, it doesn't matter. We use what we want/like and it's cool that we have choices.
However, over the past year or so I've also been looking at Ruby. Not to get into a religious argument (as you say) over which language is better, but if you like Python you should take a look at Ruby. If you're a Windows user there's an installer available, which comes with a full book (in CHM format) that can get you running in no time if you already know Python. As Perl and Python, Ruby has extensions and so on. I do like the OO features in Ruby a bit better than Python.
And least but not least, there's Lua. I wouldn't use Lua the same way I use Python, but Lua is a joy to embed, much more so than Python.
Ahhh, language wars. Cheers =)
For medium- to large-scale scientific computing, I've found that a mixture of Python and C/C++ gets me the right combination of speed and flexibility. I write the main number-crunching pieces of my code as objects in C/C++, then use SWIG to generate the wrappers necessary to interface my objects with Python. The control structures are all in Python, which is where I really want the flexibility.
Woah. I never 'got' quicksort even from reading pseudocode (call me stupid if you wish). Now I am enlightened. Amazing.
Anyway. I immediately ran off to the nearest bookstore and grabbed the first edition of the book. I read it once through and it--along with a lot of googling--helped me understand what I was doing, but once I had gone through it once I couldn't use it to recall the details of what I had been taught. If I wanted to look up something that I knew I had learned *from the book* I would have to look it up *on the web* (e.g. syntax or the required parameters of a function) because the index was useless. I never found anything I needed from that book once I did the initial once-through reading.
Though let's not gloss over the fact that I obviously learned python fairly well from this book because I did get the job! So sure, if you need to learn the language, the first edition did the job, but you'd better buy a *real* python book while you're there at the bookstore because as soon as you were done with Learning... it was nothing more than a paperweight.
If speed is critical, it might be a good idea to check out Psyco, which works sort of as a JIT compiler for Python. It's extremely simple to use in your code - just import the psyco module and make a function call to enable it. From what I've seen, Psyco typically makes Python code about three times faster. With "low-level" code, e.g. code that mostly performs arithmetic on ints and could be converted line-by-line to C code, the speedup is greater than that, easily ten times in realistic cases.
Well, most people use python to write scripts that are smaller than what they would write in C++ or java.
Please don't confuse performance and size. Larger systems don't require bigger performance, performance is needed in tight inmost loops. And those you can implement in C while retaining the rest of the Python code.
Save your wrists today - switch to Dvorak
actually an enormous game has been written in "stackless" python: EVE online. the reviews of EVE have been mediocre, but most have complimented the game's graphics. it's also an enormous world with all kinds of complex rules. according to the developers, it wouldn't have been possible without python.
i think EVE proves that python is ready for big projects, even when performance is critical.
What I'm kinda curious about is whether they use mod_python. From the looks of the URL's on Adsense and AdWords it seems likely that they are using the Publisher handler, but I've never heard any official (or even rumor) about it.
There's nothing wrong with ActiveState, except that they lag behind the main *nix releases and are generally slow to incorporate fixes. It also ships with a bunch of stuff you might not necessarily want. For example, the COM extensions. The fact that I'm running in Windows doesn't necessarily mean I want to use COM. It also takes way too long to install, considering what it is.
The Win32 Python distribution on the other hand is pretty much sync'ed with the primary *nix point releases, and in my experience as a whole the folks that write Python are much more receptive to bug reports for the Windows distribution that the Perl people (well, they are not involved with the Win32 port at all). Just try to ask a question about ASPerl on one of the Perl mailing lists or newsgroups. Not a good experience. I'd dare say the Perl community would rather ActiveState not publish ASPerl at all. The attitude from the Python camp is quite different.
Just my opinion of course.
Here's a program I've written in ruby, just today, in about 2 hours.
It generates a graphical waveform from the component harmonics, as well as graphing the first 10 harmonics.
Source Code: http://tylere.com/waveform.rb.txt
Some Examples:
Square Wave
A Phased Sine Wave
Triangular Wave
Sawtooth Wave
TODO: Something witty here...
Pyrex