views:

1016

answers:

6

I'm basically wondering if Python has any OOP shortcomings like PHP does. PHP has been developing their OOP practices for the last few versions. It's getting better in PHP but it's still not perfect. I'm new to Python and I'm just wondering if Python's OOP support is better or just comparable.

If there are some issues in Python OOP which don't follow proper OOP practices I would definitely like to know those. PHP for instance, doesn't allow for multiple inheritance as far as I'm aware.

Thanks Everyone!

Edit: How about support for Public and Private? or support of variable types. I think these are important regarding building OOP software.

+20  A: 

I would say that Python's OOP support is much better given the fact that it was introduced into the language in its infancy as opposed to PHP which bolted OOP onto an existing procedural model.

Andrew Hare
Interesting. I thought that classes were added relatively late in Python's development, but a read of Guido's blog says it was added before the first public release.
Jason Baker
Jason: They have been revised though.
troelskn
1+ for Python. OOP in PHP is a mess. I'm not trying to promote Python to other developers (ie, less work for me) but I started with PHP and after coding in Python I wouldn't take a PHP project for less than 5x its real cost (ie, a small dynamic website in CodeIgniter for 50k, no exaggeration). Nobody would pay that much though, so I think you get the picture.
orokusaki
@orokusaki You'd charge $10,000 for a small dynamic website in python?
Carson Myers
Yes. I'm a capitalist. (by small, I don't me a 5 page website brochure, I mean a CMS based website in Django with custom functionality, I just don't mean a full scale software package).
orokusaki
@orokusaki: In what way is OOP in PHP (5.3) a mess? Compared to Python, it has abstract classes, abstract methods, interfaces, public, private and protected modifiers for both methods and properties, the final modifier...
tomp
+1  A: 

I think they're comparable at this point. As a simple test, I doubt there's any pattern in Design Patterns or Patterns of Enterprise Application Architecture, arguably the two most influential books in OOP, that is impossible to implement in either language.

Both languages have come along by leaps and bounds since their infancies.

As far as multiple inheritance, it often creates more problems than it solves, and is, these days, commonly left out of languages as an intentional design decision.

Triptych
You can also implement any design pattern in C. Is C as object-oriented as Python?
Bastien Léonard
Um. The link to Wikipedia article describes the issues that MI has, and how they are dealt with in various languages. Python's solution (the MRO) is also described in the article. I am not sure what you accomplish by linking to the article.
ΤΖΩΤΖΙΟΥ
+8  A: 

Python's OOP support is very strong; it does allow multiple inheritance, and everything is manipulable as a first-class object (including classes, methods, etc).

Polymorphism is expressed through duck typing. For example, you can iterate over a list, a tuple, a dictionary, a file, a web resource, and more all in the same way.

There are a lot of little pedantic things that are debatably not OO, like getting the length of a sequence with len(list) rather than list.len(), but it's best not to worry about them.

Kiv
By "debatably not OO" do you mean "not typical OO syntax?" It's clearly OO, since it's the __len__ method of the object.
S.Lott
@S. Lott — I suspect that's exactly what he means. The implementation is properly OO, but the syntax isn't. Sequence length is an aspect of the sequence object; ideally, the language syntax would express it in that manner.
Ben Blank
@Ben: Isn't the implementation more important than the syntax? Or do you consider operator overloading "not proper OOP", because the syntax is wrong? Would you prefer 1.minus() to -1 because it is more OO? I really fail to see how people can complain about the len issue.
nikow
I like the OO aspects of Python, but I sure am tired of forgetting to type "self" in my method signatures.
Glenn
@Glenn: If you use Pydev in Eclipse, it will type self for you in method signatures.
Kiv
+3  A: 

Also: Python has native operator overloading, unlike PHP (although it does exist an extension). Love it or hate it, it's there.

Emil H
+1  A: 

If you are looking for "more pure" OOP, you should be looking at SmallTalk and/or Ruby.

PHP has grown considerably with it's support for OOP, but because of the way it works (reloads everything every time), things can get really slow if OOP best practices are followed. Which is one of the reasons you don't hear about PHP on Rails much.

Brent Baisley
And also because PHP's metaprogramming abilities can't match Ruby's or Smalltalk's, or even Python's.
musicfreak
+2  A: 

One aspect of Python's OOP model that is unusual is its encapsulation mechanism. Basically, Python assumes that programmers don't do bad things, and so it doesn't go out of its way to any extent to protect private member variables or methods.

It works by mangling names of members that begin with a two underscores and ending with fewer than two. Such identifiers are everywhere changed so that they have the class name prepended, with an additional underscore before that. thus:

class foo:
    def public(self):
        return self.__private()
    def __private(self):
        return 5

print foo().public()
print foo()._foo__private()

names beginning and ending with two (or more) underscores are not mangled, so __init__ the method python uses for constructing new instances, is left alone.

Here's a link explaining it in more detail.

TokenMacGuy
Other way around -- names beginning with two underscores (and ending less than 2 underscores) are mangled, but other names are not.
mipadi
@TokenMacGuy: please fix your answer rather than say it's "a little wrong'.
S.Lott
properties can be used to control how attributes are accessed. But sometimes I'd like to have true private attributes.
Bastien Léonard
@S.Lott: Is it still wrong?
TokenMacGuy