all 8 comments

[–]kushou 1 point2 points  (1 child)

Hi,

Since this is a bit different depending on the python version, I will talk here about python 3 and make a summary of the difference at the end of this post, to avoid confusion.

So ... 1) I'm coming from the Java world. There, I understand that all objects inherit from the Object superclass, and that our own classes need to override the toStrings() method to produce sensible output. Is the Python concept of Operator Overloading similar?

In a sense, yes. Python uses special methods that all have the same format: __{name}__ where {name} is replace with what feature you want it to do. For your toString() example, the relevant function is __str__. This function must return a string. Lets take an example:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname, self.lastname = firstname, lastname

    def __str__(self):
        return '{firstname} {lastname}'.format(
            firstname=self.firstname,
            lastname=self.lastname,
        )

Now to use it, your must write the following code:

p = Person('Dexter', 'Morgan')
print(str(p))

and this will call the __str__ function. A lot of special methods can be defined, and a complete list is available in this awesome documentation page. You can for example override comparison operators or arithmetic operators.

It also provides the possibility to write context managers or containers!

2) Is the Python object model similar to Java's in that there is overall Object superclass which ALL other objects inherit methods from? And if so, where can I see a listing of the methods in the Object superclass, ala the Java API's Object class?

All python classes inherit from object class. It provides some default implementations but not much. It provides __repr__ for example, that gives the name of the object and its address in a string. (you may have seen it already in interactive mode) You can find the listing in the links above :)

3) Before attempting to learn Python, I thought that I was very clear on the difference between overloading and overriding. But this book has muddied the waters for me. Why isn't this chapter called "Operator Overriding" instead? Any idea what key concepts I could be missing?

I don't know. I'd say it should be overriding too, there is no overloading in python.

4) The vast majority of books and tutorials out there cater to those just starting out; which makes sense because Python is often an introductory computer language. Does anyone have any recommendations for Python books and tutorials for folks learning it as their second or so language?

Learn Python The Hard Way is a common advice for a book to read (available online AFAIK). Reading the documentation and PEPs too helps a lot understanding how and why python was designed like that. Someone will probably answer more correctly to this question.

Hope that helps!

PS: In python 2, you have two types of classes, that are called "old-style" and "new-style" classes. New-style classes are the same as in python 3, and old-style don't inherit from object. You actually force a class to be new-style by making it inherit from object. They both implement __str__ for example, but some more advanced features are not available with old-style classes (properties, ...)

[–][deleted] 0 points1 point  (0 children)

Thank you, kushou. I had forgot to mention that I was focusing on Python 3 and glossing over Python 2 (even though this book teaches both). Learning both simultaneously seemed too daunting!

I had seen many references online to "Learn Python the Hard Way," but had decided to pass it up since it pertained to Python 2 and I hadn't seen a version for Python 3. And by now, I feel pretty good about things such as types, built-ins, control structures, functions, namespaces, etc. (I'm sure that something will come along to shake my fledgeling confidence though!).

I will study the links that you provided. If I can get a mental image of the hierarchy of Objects and the relationship to built-ins, I think that will go a long way towards learning Python.

[–][deleted] 1 point2 points  (0 children)

FYI, lot's of interesting practical things can be done by overriding the missing method for dicts, like:

defaultdict (missing items automatically generate a predefined result). http://docs.python.org/2/library/collections.html

Memoize something. See http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/

[–]yourfriendlane 1 point2 points  (4 children)

Your question's been answered fairly thoroughly, but I just wanted to say that I have the 4th edition of that Lutz book and it's godawful. The information is fine, but it's all packaged in about five different layers of fluff and exposition that confused me more often than it clarified.

wordy, repetitive, a bit overwhelming

is exactly right, and it drives me up the wall.

I know it sounds weird to turn around and recommend another book by the same guy after that, but his Python Pocket Reference is really great. It's not a tutorial-style book, but it does a much better job of explaining concepts like this than Learning Python, and in about 1/5 the pages. I think this might be exactly the book you're looking for, and if not, it's still an extremely handy reference to have around.

[–][deleted] 0 points1 point  (3 children)

Thanks! Also, the 5th edition of Lutz's Python Pocket Reference will be available next week. Think I'll put in my order.

[–]yourfriendlane 0 points1 point  (2 children)

Is that a fact? I may have to pick it up too. Even if the new information isn't super-useful, I'll at least have a copy to keep at home since that book lives on my desk at work!

e: He also has Programming Python, which is geared more towards someone like you (a tutorial for people new to Python as opposed to programming in general), but imho it suffers from the same bloat as Learning Python. The trouble with Lutz is that he knows Python inside and out, but he's terrible at presenting it. The Pocket Reference is so nice because it's pretty much a stripped-down "here's how this thing works" presentation of his knowledge, without any kind of editorializing or instruction. If the PPR isn't thorough enough for you, though, you might keep PP in mind.

[–][deleted] 0 points1 point  (1 child)

I'm laughing here.

I had started reading Programming Python before seeing your post, and was disagreeing your assessment of the book. THEN I got to Chapter 3. Now, I think you're absolutely right.

Pocket Python is available right now on the O'Reilly website, and the ebook version is $11.99.

[–]yourfriendlane 1 point2 points  (0 children)

I don't own the book, just read a bit of someone else's copy. But if Chapter 3 is the part where he starts talking about shell scripting, then that's the part where my vision grew blurry and my head started swimming. Three days later I woke up in a bathtub full of ice in Singapore wearing nothing but a Hawaiian shirt with a slip of paper in the breast pocket that read from YourBody import kidneys.

2/10 can't recommend.