“Learning Python, Second Edition” was published only a few months ago by O’Reilly. We take a quick look at this promising book.The book starts with an introduction of Python, it is mostly a big chapter regarding “why python?”. I must say that the advocacy doesn’t manage to excite me about Python, however I am sure younger/newer programmers might find it more useful.
I like how the two authors explain everything about Python really from step 1, e.g. about how to run python apps, what is the difference between interpreting and compiling etc.
Part II gets into basic concepts of numbers, strings, lists, directories and how they used inside Python. Then, the book discusses the actual language by explaining the loops, how to get around whitespace problems (my only gripe with Python), and it continues into more advanced topics: functions and modules.
The real meat stares at page 300 by diving into OOP and its concepts: inheritance, operator overloading etc. It is also useful for newbies to read about the difference of classes vs modules. Exceptions are discussed after that.
The rest of the book is devoted to generic programming information, e.g. the bindings, Tkinter, jython, and resources. On this last part of the book you will find my favorite chapter: “Common Tasks in Python”. This is a chapter that explores common programming tasks and then offers the solution on how to solve them. Think of it as “the little book of answers for specific tasks”. Extremely useful and uplifting for new Python programmers.
Truly, I can’t find anything wrong with this book. I think this is the best Python book I have seen around (and I own two more). IF I had to pick on something, that would be the fact that the authors don’t discuss the Python.NET, PyQt and PyGTK ports with some practical pointers and code samples to further the current 2-line mentioning of the bindings. But even this won’t change my opinion on the book: it is highly recommended, mostly for new programmers, however a bit more advanced developers might find some of its chapters useful as well.
If you think on getting into Python, this is the book to get!
Overall: 9.5/10
Buy “Learning Python“ at Amazon.com for less |
…maybe this article stood a bit too long in the queue? I have this book, second edition and everything, for some 6 months now.
Anyway, this book is very good indeed. But for those who prefer to save some money (and don’t mind to read stuff in the screen or print a lot of pages), there’s “Dive into Python” (http://diveintopython.org/toc/index.html) which is even better written, and available online.
I got the book 1 month ago.
“how to get around whitespace problems”
Care to elaborate what you mean by this? I’ve used python quite a bit in recent years, and have never had problems with whitespace as long as my code was indented properly ;-p
>never had problems with whitespace as long as my code was indented properly
Exactly. You always need to be careful about these and the blank lines. Bring *perfect* on formatting it’s too much work for many programmers. I much prefer freedom of flow personally.
Haha, very true.
I do have to say one problem that’s always bugged me about python is that commenting out an if statement can sometimes be a PITA.
For example:
if a:
b()
else:
c()
The easiest way is to change a to false like “if false: # a:”. Just not something I’m used to doing, and sometimes catches me on a large if statement. Also, I think
if a:
# do nothing
shouldn’t require a “pass” if the next line is unindented. Oh well, no language is perfect 🙂
When I first heard that Python had syntactically meaningful whitespace, I was a little apprehensive. My mind was immediately filled with all sorts of potential problems that this might cause, but I think that I was just panicking. Most programmers are used to indenting properly anyway, so it isn’t too much of an issue for small programs. However, I’ve found that it gets confusing for larger programs, but that might just be because I’m not too used to Python. (I suppose that the same thing might be said for nesting braces in C/C++.) What’d be nice is an editor that shows you visually to which block of code the current line belongs.
I picked up the same book some months ago. I thought it was pretty good, but I didn’t really get too much more out of it than the tutorials on the Python website. I also picked up the Python Essential Reference, which I use a lot more. I’d like to see an updated version, though.
You don’t have to be “perfect” in the indentation, just consistent. It’s not as half rigid as people think (not like Fortran at least), you just need to do it for half an hour and then you pretty much forget about it (especially because your editor probably takes care of the indentation for you).
The good thing about this feature is that the code looks pretty much the same for every programmer and it’s very likely to be readable, so for team programming that’s a big plus.
IMHO I understand why someone who comes from a VB background would complain a bit about it, since it helps a lot to have stuff like “If…End If” and “For..Next” to enclose your blocks, but I can’t see why Python’s style could be any worst than having a lot of meaningless curly brackets all over the place, since they don’t really give out much information (if you think about it, you end up using the indentation as a reference the same way).
Finally, I don’t know what Eugenia meant with “blank lines”…never noticed a problem with that.
Just cuz you got this book 1 month ago doesn’t mean it was released ‘a few months ago’. It was released in December 2003, over 6 months ago. By most standards that’s more than ‘a few months’.
I appreciate the review, but having had the book since February myself, I was almost hoping this would be a new edition but alas
I’ve read a bit about python and I think I could like it but I am interested in GUI programming. Is there a book doing more than an introduction to Python in that context ?
Othewise, the review is a bit short isn’t it ? But nice nonetheless.
If you know some other programming language, have a look at Dive into Python (doesn’t cover any GUI programming) http://diveintopython.org/
For GUIs:
There is the freely available PyQT book (from 2001): http://www.opendocspublishing.com/pyqt/
For GTK: http://www.pygtk.org/pygtk2tutorial/index.html
and for wxPython http://wiki.wxpython.org/index.cgi/Getting_20Started
And concerning .Net, lets just jope IronPython will be released soon, so we can use C# and Python together
For those that want python-like language for .net/mono: check out boo (boo.codehaus.org).
C# is all cool and ok …
But python is much more flexible than a statically typed language will ever be ..
Lookup what __call__ or __getattr__ does in python
Programming glade+python brings productivity to a high..
But my favourite python snippet is
a = 10
b = 20
(b,a) = (a,b)
PS: and yes, I work on dotgnu
Nice links.
What’d be nice is an editor that shows you visually to which block of code the current line belongs.
Like this this you mean –
http://www.die-offenbachs.de/detlev/images/eric3-screen-2.png
Damon
I love when people designing compilers/interpreters/languages is able to look at different ones withouth the “I am the right on” mood, kudos to you.
Anyway, you don’t need parenthesis:
a,b=b,a
and this runs fine in ruby too. but why you think __call__ is something related to dynamic typing? it’s just syntax sugar for, say, Invoke()
A week ago I bought this book. At first I though I didn’t really need an introduction, because I know the Python basics. A week later I am really convinced this book learnt me a lot. The nice thing is that it provides alot of technical background information that helps understanding how things are done, and why they are done that way.
Although Eugenias review is a bit short, I fully agree with her conclusions. Just buy this book!
Thanks all for the article, comments and links.
Because C# with .NET/Mono/dotGNU is not on every unix-like platform that I use. Right now, IMHO it is still a niche language…
And as for indenting, use EMACS and it will indent automagically.
Don’t spend your time feeding the trolls…
If you’re a veteran programmer, dive into python ( http://diveintopython.org ) is the best. The author of the article also might prefer the python cookbook to this book, as the whole book is devoted to specific solutions to a set of problems.
In writing Python programs, I’ve seldom had a problem with whitespace or indentation. It’s just utterly convenient to start out a function or block with def, for, etc. and just press return when you are finished with that block. It sure beats having to keep track of curly braces like in JavaScript, PHP, etc. A Python-oriented editor like IDLE or eric, with a highlight mode, will help keep track of this.
“Why use Python when you can just use C# with .NET/Mono/dotGNU. ”
because python run everywhere like java and are must easy to learn
Python is
mature
simple for trivial tasks
capable of complex tasks
available on many many platforms
And for me, Python is aesthetically pleasing and emotionally relaxing, freeing to work with.
I’m currently reading this book and my problem with it lack of references to the docs. So far (I’m on p. 95), they’ve been using top-level functions like len(), int(), str(), float(), list() as if they were part of some top-level module which I don’t know about and they won’t tell me. I’d have preferred it if the authors told me right off the bat how to find out what’s in the standard library, and how to get online help on how to use said library.
They even list all the string methods, but don’t give further information about how to get the docs on them.
Another beef I have is on p. 37 where, in a hand-waving argument, they seem to imply that the following terms are all the same:
library of tools
python source file
module
package of names
namespace
bunch of “attributes”
I wish they’d clearly explain the distinctions between (and definitions of) those 6 terms.
IMO, The book is *pretty good*. I’d give it a 6.5/10.
I’ve tried Python in my spare time, mainly because I’m a developer and I like to try out new languages; but I’ve never seen it actually used in any of the work environments I’ve been in. Is there a particular industry it’s popular in? I did a search of the Atlanta area for Python on computerjobs.com and got 1 hit for a web developer. Just wanted to know what people are mainly using it for.
“You don’t have to be “perfect” in the indentation, just consistent. It’s not as half rigid as people think (not like Fortran at least), you just need to do it for half an hour and then you pretty much forget about it.”
Funny… Being mainly a c/c++/c#/java programmer for years, i’ve been doing mostly php the past few months, and i still forget to put dollar signs $ in front of the variables… several times a day
Anyway, programmers are picky when it comes to indentation, i myself would kill any c programmer that doesn’t put curly brackets { on separate lines, people who put them on the sames lines as the keywords, functions or whatever just annoy the crap out of me.
> Just wanted to know what people are mainly using it [Python] for.
I think it’s gaining popularity in a backroom sort of way, the way GNU/Linux gained popularity: your boss (if you’re employed) needs you to get something done but doesn’t micromanage you. You choose Python, deliver your boss’s solution, and he or she is happy. Next thing you know, half the web site’s back-end is in Python, along with various scripts scattered all over the system(s). Maybe there’s a big long Perl script that needs fixing, and you can’t wrangle the thing, so you rewrite it in Python. [shrug]
I used Python/MS-SQL to replace an aging VB/MS Access program that extracts a ton of data from an oracle DB and normalizes it into a catalog for our customers.
I also added a nice wxWindows gui to it and made it so that all the steps in the catalog generation could be run separately, one at a time, for debugging purposes. Very nice! Might I add that it also ended up running in 1/2 the time of the VB/Access program.
It would process well over a million records, pulling info from a dozen tables with hyper complicated queries (our database system is over-flexible – needs some flattening) and takes about 14 to 16 hours to run, total. The old program took 24 hours.
I’ve been using it for over a year, but it is about to be retired… We found a better way of getting the info. to our customers.
Mike
…alright, not HALF the time. But significantly LESS time.
> Just wanted to know what people are mainly using it [Python] for.
A large portion of the Python users I see are (non-software) engineers and scientists who are using python to tie together lower level hardware interfacing or computation routines into a tool for their own work.
I think this Python adoption in this group of people is higher becasue they are typically: comfortable with C (or Fortran), have legacy/third pary code they want to call, want OO and procedural design methodologies available, are not releasing their software as a boxed up product for sale, and want a faster design cycle. This is the same group of people, though different individuals, that use Tcl/Tk to keep their labs running.
Also, a lot of scientists and engineers take one look at the Numeric or Numarray package along with something like IPython and are sold. The packages that make things more MATLAB-like win a lot of points.
We are using Python quite successfully in an enduser desktop application. The main program is in C++ and delegates some of the more complex tasks to Python running in an embedded interpreter. This is absolutely invisible to the enduser, unless they look into the install folder and see python23.dll, they’d never know.
Overall I like Python very much. It isn’t the right tool for every job, but the right tool for lots of jobs.
About previously discussed indention… Any programmer worth his money should be able to handle that. I didn’t like it first, but meanwhile I prefer the Python way. As a sidenote, I seem to be indenting my C++/Java code more properly now than before after developing that good habit working with Python.
Concerning speed… this is mostly irrelevant. And there is Psyco which can speed up data manipulation drastically.
john-
open a python interpreter and type :dir(__builtin__)
to get a list of builtin functions.
to get documentation for said functions, say map type:
map.__doc__
hth
I was looking at this book yesterday, trying to decide whether or not I should get it. I had been leaning toward buying it, and wanted to check online reviews. I think that I will buy it now; thank you.
Thanks.
>>> print fn_name.__doc__
works, but dir(__builtin__) does not:
>>> dir(__builtin__)
Traceback (most recent call last):
File “<stdin>”, line 1, in ?
NameError: name ‘__builtin__’ is not defined
Make an “import __builtin__” first…
Just use dir(). Also help(). Besides, the tutorial and especially the language reference are your friends.
I found python (and wxPython) very useful in situations where I had to interface several different different ways of doing things work together, and get it done quickly.
In a manufacturing environment I had a short time to interface with a DDE server to acquire info from everal pieces of machinery, kick off an alarm on an IO board controlled by another DDE server and store info through an ODBC interface into a database and provide a nice attractive GUI, in a couple of days, with never having used DDE before.
I would be still working on it if I had done this in C++.
Python makes these kind of situations very easy, and simple. The code was only a few pages, mostly various wx controls.
I sympathize with your cause.
Shortly after I started to learn python, I discovered ruby, which then quickly became my favorite programming language (and now has make me all the more reluctant to dive into python.) The way the two languages organize themselves seems to highlight your point about python’s top-level functions.
I might get into trouble for saying this, but I agree with you in that it feels like python’s top-level functions appear (in your words) “as if they were part of some top-level module which I don’t know about and they won’t tell me.”
Sure, you can do all the nice OO stuff in python, but when you’re “thinking in python”, you sometimes have to break out into a “functions and procedural programming” mindset just to make simple expressions. By contrast, in ruby, *everything is an object*, and behaves purely as such. You don’t have to guess if something is treated as a function or a method call. You also don’t have to put all those dumb “self”s inside classes.
Simple Example:
In python:
print len(“This is a string”)
In ruby:
puts (“This is a string”).length
In the python statement, we call on our built-in function len(), using our string as the parameter. In ruby, our string is an object, so we simply evoke it’s “length” behaviour. (And if we wish to know any of the other behaviours that are available from strings, a simple look at the reference under String class will do the trick, see: http://www.rubycentral.com/book/ref_c_string.html)
Though an infinitely simple example, it represents ruby’s consistency in treating all things as objects. If there is anything like python’s top-level functions in ruby, it would be the Object superclass
(see http://www.rubycentral.com/book/ref_c_object.html), from which all objects are derived.
Well, so there you have it. Nuts that my ruby scripts may never be able to be compiled into bytecode (like python can), and nuts that they will probably never run as fast as python code, nor have access to GTK+ or wxWidgets GUI toolkits, nor all the tools that python has. Oh, ruby development may never catch up to python, so I guess I’ll have to learn python. Anyone out there interested in making a ruby-like dialect that maps onto python bytecode?
Pleeeeeese
I agree that Ruby is a great language, and it’s great to read an honest, non-flame comparison like yours, for a change.
I kinda love both, so my heart is divided..but I think it’s important to note that the fact that in Python “not everything is an object” is good when it comes to teaching, since Python allows a teacher to first teach it as a procedural, traditional programming language, and then introduce OO programming.
It might be a bit harder for a beginner to grasp programming, algorithms, data structures _and_ OO all at the same time, as it happens with pure-OO languages.
Anyway, both are fantastic I wish the Ruby community as “vibrant” as the Python though… (my personal experience of course, maybe not all would agree)
I wish the Ruby community were as “vibrant”
Sorry
I appreciate your feedback and comments.
Thanks.
> Simple Example:
> In python:
>print len(“This is a string”)
> In ruby:
> puts (“This is a string”).length
If you really like methods better than functions, Python won’t stop you from writing:
print “This is a string”.__len__()
Don’t even need the extra parens around the string.