is python Object oriented??

Michael Torrie torriem at gmail.com
Thu Jan 29 12:19:25 EST 2009


M Kumar wrote:
> but still I am not clear of the execution of the code, when we write or
> execute a piece of python code without defining class, predefined class
> attributes are available (not all but __name__ and __doc__ are available).
> does it mean anything to this topic. Is it necessory to have __module__,
> __dict__ and __bases__ for a class object in python?

I think you're confused as to what object-oriented means.  OO defines
the internals of a language more than a particular programming paradigm.
 Obviously python lets you program in a variety of paradigms, including
procedural and event-driven, but it is all very much object-oriented.
So ignore those that say python doesn't force you to use OOP, when in
fact it's unavoidable.  It's just that you're not forced to place all
your code in class definitions.  You don't need to because your code is
already object-oriented in that you're manipulating objects and their
attributes.

As others have said, Python is an object-oriented language through and
through, closer to Smalltalk in many ways, the grand-daddy of all OO
languages.

It appears that you are only really familiar with Java, and that leads
to a number of interesting misconceptions about OO.  Java's bizarre OO
requires everything to be in a class, which leads many people to believe
this is what OO should be.  In fact Java is a bit odd when it comes to
OO, as there are many things in Java that aren't in fact objects.  For
example, primitives are intrinsic types and are not objects.
Furthermore class definitions are not objects either, at least from the
programmer's pov.  You can't manipulate them by standard means as you
can in Smalltalk and Python.  In Smalltalk and Python a "class" is an
object just as much as an instance of a class is an object which has a
constructor factory method that returns instance objects.  Java also has
very strange ways of doing singleton patterns.  You have to wrap
singletons in class and define them as "static."  I think this was
inherited from C++.

The most basic object in a python script is the module object which
represents the namespace of the current script.  In effect a module
object is a singleton.  It has a few attributes, and you can use it to
look up any of the objects it contains, such as functions, objects
(so-called variables), classes, etc.  Everything in python is an object.
 The statement:

a = 4

defines an integer object "4" and binds a name to it, 'a.'  You can even
check to see what methods the object supports by doing:

>>> dir(4)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

dir(a) would return the same thing.  As you can see, all the operators
that can be performed with a number object are defined.  This little
exercise alone should show you how much more object-oriented Python is
than Java.

Python's OO capabilities are really exposed when you start extending
built-in types, or doing meta programming where you dynamically alter
classes (and instance objects) on the fly.



More information about the Python-list mailing list