tags:

views:

522

answers:

6

I remember that at one point, it was said that Python is less object oriented than Ruby, since in Ruby, everything is an object. Has this changed for Python as well? Is the latest Python more object oriented than the previous version?

+10  A: 

I'm not sure that I buy the argument that Ruby is more object-oriented than Python. There's more to being object-oriented than just using objects and dot syntax. A common argument that I see is that in Python to get the length of a list, you do something like this:

len(some_list)

I see this as a bikeshed argument. What this really translates to (almost directly) is this:

some_list.__len__()

which is perfectly object oriented. I think Rubyists may get a bit confused because typically being object-oriented involves using the dot syntax (for example object.method()). However, if I misunderstand Rubyists' arguments, feel free to let me know.

Regardless of the object-orientation of this, there is one advantage to using len this way. One thing that's always annoyed me about some languages is having to remember whether to use some_list.size() or some_list.length() or some_list.len for a particular object. Python's way means just one function to remember

Jason Baker
Why not expose .len() directly off of list then? I think you can't completely divorce OO design from the syntax, because the syntax, to a large extent, defines your code paradigm. some_list.len() is OO because you are thinking about the list as an object that will be able to tell you what its length is. len(some_list) is not OO, regardless of what it translates to under the covers.
James McMahon
Sorry. Syntax and OO have nothing to do with each other. Python objects can have non-object syntax. They're still objects.
S.Lott
+1 This is a better answer.
Andrew Hare
@nemo - I'd disagree with your comment, but such is life. There is one advantage to using Python's syntax outside the oo-ness. I edited that in to my answer.
Jason Baker
object oriented includes both syntax and data model, but object based includes data model and if possible syntax sugar.
mtasic
@S.Lott, if your not treating an object as an object by calling methods off that object to deal with said object. You are not programming in the OO paradigm. That is what I meant by syntax, the ability to treat objects as objects. As mtastic said, OO is a combination of syntax and the underlying data model. If you have a dog object and you pass it to a method run(dog), then not programming in a OO style, which would be dog.run(). The dog is told to run directly. Don't confuse what I'm saying for putting any paradigm down, they all have their place, but by definition, run(dog) is not OO.
James McMahon
Since len(list) invokes a method of the list, I don't see where the object-orientation vanished. I am treating the list as an object, since everything's an object. This isn't C++.
S.Lott
I think we are confusing the data type with the paradigm. What I am talking about when I say syntax is the application of paradigm. For instance, in Java, where you have primitives and objects. You can still treat and object (like a String) in a non OO way. Like normalize(string) versus string.normalize. String in both case is an object, but only in the second case are you using the OO paradigm, where the first is more like C style procedural code.
James McMahon
Forgive me, I'm not a python expert, but I don't see much discussion of true "object orientedness". Is len(some_list) polymorphic in python? Can I subclass off of that list and override the len method? Just curious.
Ogre Psalm33
@Ogre Psalm33 - Yes. If you override the __len__ method of the object, it will control what the len function gets.
Jason Baker
I don't think dot notation and other common syntaxes are essential to the concept of OOP. Consider: multiple dispatch (multimethods) http://en.wikipedia.org/wiki/Multiple_dispatch
Doug
+2  A: 

Hold on, both Ruby and Python are object oriented. Objects are objects. There isn't more object oriented 'comparison function' that will lead you to the better one. Syntax is not only thing which makes some language to look like object oriented one, but also data model.

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.) http://docs.python.org/reference/datamodel.html

mtasic
+28  A: 

Jian Lin — the answer is "Yes", Python is more object-oriented than when Matz decided he wanted to create Ruby, and both languages now feature "everything is an object". Back when Python was younger, "types" like strings and numbers lacked methods, whereas "objects" were built with the "class" statement (or by deliberately building a class in a C extension module) and were a bit less efficient but did support methods and inheritance. For the very early 1990s, when a fast 386 was a pretty nice machine, this compromise made sense. But types and classes were unified in Python 2.2 (released in 2001), and strings got methods and, in more recent Python versions, users can even subclass from them.

So: Python was certainly less object oriented at one time; but, so far as I know, every one of those old barriers is now gone.

Here's the guide to the unification that took place:

http://www.python.org/download/releases/2.2/descrintro/

Clarification: perhaps I can put it even more simply: in Python, everything has always been an object; but some basic kinds of object (ints, strings) once played by "different rules" that prevent OO programming methods (like inheritance) from being used with them. That has now been fixed. The len() method, described in another response here, is probably the only thing left that I wish Guido had changed in the upgrade to Python 3.0. But at least he gave me dictionary comprehensions, so I won't complain too loudly. :-)

Brandon Craig Rhodes
Good direct answer.
James McMahon
Did Guido ever give a reason why he left out the len() method in 3.0?
James McMahon
Yes, there is an article somewhere. Basically his reasning was that len(my_list) is more straigthforward to read than my_list.len().
nikow
@nemo, basically he said that len(x) is guaranteed to return an int, whereas x.len() has no such guarantee. And, that he originally liked the way it looks better. (found the emails: http://mail.python.org/pipermail/python-dev/2008-January/076575.html http://mail.python.org/pipermail/python-dev/2008-January/076612.html)
JimB
+2  A: 

This is an incorrect belief.

See my previous answer here for more in-depth explanation:

http://stackoverflow.com/questions/865911/is-everything-an-object-in-python-like-ruby/865963#865963

Why not expose .len() directly off of list then? I think you can't completely divorce OO design from the syntax, because the syntax, to a large extent, defines your code paradigm. some_list.len() is OO because you are thinking about the list as an object that will be able to tell you what its length is. len(some_list)

.len() is available directly off the list. It is available as __len__(). len() is a function object. You can see all its methods with dir(len). While I do not know why Guido decided to make the __len__() method longer, it does not change the fact that all of those are still objects.

Unknown
Thanks for the direct reply. I though calling __method__ directly was discouraged in Python as __ was shorthand for private. I think I am just confusing the __ with something else.
James McMahon
Ah __method is distinct from __method__, the former is private (unenforced) and the latter is "special system functions with pre-defined behavior".
James McMahon
+6  A: 

Although this is not properly an answer... Why do you care about Python being more or less OO? The cool thing about Python is that it's pythonic, not object oriented or funcitonal or whichever paradigm that is fashionable at the moment! :-)

I learnt to program with Java and Object Orientation, but now I don't give a sh.t about it because I know that OOP is not the solution to all the problems (indeed, no single paradigm is).

see:

fortran
That OOP fad will never last. What has it been, three decades?
Glenn
+1  A: 

I have the same "perception" perhaps derived from this:

Why was python created in the first place:

It occurred to me that a scripting language with a syntax like ABC [...] would fill the need

An Interview with the Creator of Ruby:

"I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python

I know that perception is not the same as reality. Both Python and Ruby are great programming languages and both are very OO.

OscarRyz