9

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?

6
  • 7
    In Python, everything's an object. What source did you read? Can you provide a URL or a quote? Commented May 21, 2009 at 18:59
  • 5
    @S.Lott — He's right; in fact, the Ruby "About" page still recounts that as the origin of Ruby. Matz is quoted as saying: “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.” ruby-lang.org/en/about Commented May 21, 2009 at 19:27
  • I have the same perception. Python being more "structured" and Ruby born to be pure OO since its inception. But I cannot tell why.. Commented May 21, 2009 at 19:34
  • In Ruby some things are not objects. They are disguised as objects, and (mostly) act as objects, but they aren't. Don't believe me? Try to define a singleton method for the number 3 (a = 3; def a.foo ; end) and the sharp edges of a non-object will show. Commented Mar 2, 2010 at 22:00
  • 1
    @banister, Yours is a different way to look at it than I had considered, and not necessarily wrong. From the Ruby source, we know that most variables are held as references which essentially point to the memory containing the object, but for some types, such as Fixnum, the value of the object is in the reference itself. That's why you can't define a singleton on 4: There's no normal instance to which the singleton can be attached. Rather than consider "4" as an object with special restrictions, it seems to me a "non object" made to look mostly like an object. Commented Sep 8, 2010 at 14:15

6 Answers 6

40

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. :-)

Sign up to request clarification or add additional context in comments.

4 Comments

Did Guido ever give a reason why he left out the len() method in 3.0?
Yes, there is an article somewhere. Basically his reasning was that len(my_list) is more straigthforward to read than my_list.len().
@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: mail.python.org/pipermail/python-dev/2008-January/076575.html mail.python.org/pipermail/python-dev/2008-January/076612.html)
Oh, wow. A really great blog post on the subject of why len() is a function instead of a per-object attribute: lucumr.pocoo.org/2011/7/9/python-and-pola
12

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

10 Comments

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.
Sorry. Syntax and OO have nothing to do with each other. Python objects can have non-object syntax. They're still objects.
object oriented includes both syntax and data model, but object based includes data model and if possible syntax sugar.
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 - Yes. If you override the len method of the object, it will control what the len function gets.
|
7

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:

1 Comment

That OOP fad will never last. What has it been, three decades?
2

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

Comments

2

This is an incorrect belief.

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

Is everything an object in python like ruby?

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.

2 Comments

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.
Ah method is distinct from __method, the former is private (unenforced) and the latter is "special system functions with pre-defined behavior".
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.