Objects
Python supports most object oriented programming techniques. It allows polymorphism, not only within a class hierarchy but also by duck typing. Any object can be used for any type, and it will work so long as it has the proper methods and attributes. And everything in Python is an object, including classes, functions, numbers and modules. Python also has support for metaclasses, an advanced tool for enhancing classes' functionality. Naturally, inheritance, including multiple inheritance, is supported. It has limited support for private variables using name mangling. See the "Classes" section of the tutorial for details. Many Python users don't feel the need for private variables, though. The slogan "We're all consenting adults here" is used to describe this attitude. Some consider information hiding to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals. However, the strongest argument for name mangling is prevention of unpredictable breakage of programs: introducing a new public variable in a superclass can break subclasses if they don't use "private" variables.
From the tutorial: As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to "break into the definition."
OOP doctrines such as the use of accessor methods to read data members are not enforced in Python. Just as Python offers functional-programming constructs but does not attempt to demand referential transparency, it offers an object system but does not demand OOP behavior. Moreover, it is always possible to redefine the class using properties so that when a certain variable is set or retrieved in calling code, it really invokes a function call, so that spam.eggs = toast
might really invoke spam.set_eggs(toast)
. This nullifies the practical advantage of accessor functions, and it remains OOP because the property eggs
becomes a legitimate part of the object's interface: it need not reflect an implementation detail.
In version 2.2 of Python, "new-style" classes were introduced. With new-style classes, objects and types were unified, allowing the subclassing of types. Even entirely new types can be defined, complete with custom behavior for infix operators. This allows for many radical things to be done syntactically within Python. A new method resolution order for multiple inheritance was also adopted with Python 2.3. It is also possible to run custom code while accessing or setting attributes, though the details of those techniques have evolved between Python versions.
Read more about this topic: Python Syntax And Semantics
Famous quotes containing the word objects:
“Though collecting quotations could be considered as merely an ironic mimetismvictimless collecting, as it were ... in a world that is well on its way to becoming one vast quarry, the collector becomes someone engaged in a pious work of salvage. The course of modern history having already sapped the traditions and shattered the living wholes in which precious objects once found their place, the collector may now in good conscience go about excavating the choicer, more emblematic fragments.”
—Susan Sontag (b. 1933)
“If the Christ were content with humble toilers for disciples, that wasnt good enough for our Bert. He wanted dukes half sisters and belted earls wiping his feet with their hair; grand apotheosis of the snob, to humiliate the objects of his own awe by making them venerate him.”
—Angela Carter (19401992)
“But after the intimacy-inducing rituals of puberty, boys who would be men are told we must go it alone, we must achieve our heroism as the Lone Ranger, we must see the other men as threats to our masculine mastery, as objects of competition.”
—Frank Pittman (20th century)