Has Python changed to more object oriented?

2020-06-16 04:08发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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:

  • The Zen of Python
  • What is Pythonic
  • Be Pythonic


回答4:

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



回答5:

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.



回答6:

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.



标签: python ruby oop