Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has Python changed to more object oriented?

Tags:

python

oop

ruby

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?

like image 271
nonopolarity Avatar asked May 21 '09 18:05

nonopolarity


2 Answers

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

like image 98
Brandon Rhodes Avatar answered Sep 25 '22 02:09

Brandon Rhodes


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

like image 37
Jason Baker Avatar answered Sep 26 '22 02:09

Jason Baker