Why python? Python has become very popular among developers, who are attracted to its clean syntax and reputation for productivity.
Python is weakly typed lenguage. In a strongly typed language, each variable has a type that's known at compile time. If I attempt to invoke a method on a variable whose type doesn't declare that method, the compiler of a strongly typed language tells me of the error. That's one difference between a strongly typed and weakly typed language.
In a strongly typed language, a variable's type implies the object it references will have a particular interface. That interface doesn't just tell me at compile time what method signatures exist, it also tells me what those methods mean. It tells me what the methods promise to do.
Checking the type of an object in python at runtime is rarely done, you invoke the method without guarantee.
In Python, there is a contract, but the contract is implicit. The contract isn't specified by an interface.
Python objects is that they are not merely instances of their classes; their structures can change in runtime. This level of flexibility combined with the ability of the instance to trap attribute access lays a foundation for various Python idioms. On the other hand it also requires some restraint from the programmer, because overly ``dynamic'' code can be very difficult to understand and maintain.
Python is dynamically but strongly typed. The fact that the two halves of that statement fit together can confuse those who come from a static language type background. In Python it is perfectly legal to do this :
Duck typing :
If it walks like a duck and quacks like a duck, I would call it a duck.
In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. For example, in a non-duck-typed language, one can create a function that takes an object of type Duck and calls that object's walk and quack methods. In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error.
Python supports a limited form of multiple inheritance as well. A class definition with multiple base classes looks as follows:
class DerivedClassName(Base1, Base2, Base3):
.
.
.
The only rule necessary to explain the semantics is the resolution rule used for class attribute references. This is depth-first, left-to-right. Thus, if an attribute is not found in
DerivedClassName, it is searched in
Base1, then (recursively) in the base classes of
Base1, and only if it is not found there, it is searched in
Base2, and so on.
(To some people breadth first -- searching
Base2 and
Base3 before the base classes of
Base1 -- looks more natural. However, this would require you to know whether a particular attribute of
Base1 is actually defined in
Base1 or in one of its base classes before you can figure out the consequences of a name conflict with an attribute of
Base2. The depth-first rule makes no differences between direct and inherited attributes of
Base1.)
It is clear that indiscriminate use of multiple inheritance is a maintenance nightmare, given the reliance in Python on conventions to avoid accidental name conflicts. A well-known problem with multiple inheritance is a class derived from two classes that happen to have a common base class. While it is easy enough to figure out what happens in this case (the instance will have a single copy of ``instance variables'' or data attributes used by the common base class), it is not clear that these semantics are in any way useful.
Predefined Class Attributes
Classes have five predefined attributes:
| Attribute | Type | Read/Write | Description |
| __dict__ | dictionary | R/W | The class name space. |
| __name__ | string | R/O | The name of the class. |
| __bases__ | tuple of classes | R/O | The classes from which this class inherits. |
| __doc__ | string OR None | R/W | The class documentation string. |
| __module__ | string | R/W | The name of the module in which this class was defined.
|