How does Python OOP compare to PHP OOP? How does Python OOP compare to PHP OOP? php php

How does Python OOP compare to PHP OOP?


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.


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.


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 5print 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.