python without OO
beliavsky at aol.com
beliavsky at aol.com
Wed Jan 26 22:55:40 EST 2005
John Hunter wrote:
> >>>>> "Davor" == Davor <davorss at gmail.com> writes:
>
> Davor> not really - it was not my intention at all - but it seems
> Davor> people get upset whenever this OO stuff is mentioned - and
> Davor> what I did not expect at all at this forum as I believed
> Davor> Python people should not be so OO hardcore (seems not all
> Davor> as quite a few have indicated in their
> Davor> replies)... Nevertheless, I think the discussion has
> Davor> several quite good points! --
> Davor> http://mail.python.org/mailman/listinfo/python-list
>
> Consider the case of a list, say
>
> x = [1,2,3,4]
>
> suppose you wanted to reverse the list, so that x becomes [4,3,2,1].
> In a procedural language, one might do
>
> x = reverse(x)
>
> In an OO language such as python, one might do
>
> x.reverse()
>
> Is the OO way more obscure and complicated, etc? Not really -- it's
> only a minor syntactical difference. One of the core ideas behind OO
> programming is that data (the contents of the list 1,2,3,4) and
> methods (sorting, reversing) are bound together into a single entity,
> the object. On the face of it, this is rather sensible.
I think the OO way is slightly more obscure. It's obvious what x =
reverse(x) does, but it is not clear unless you have the source code
whether x.reverse() reverses x or if it returns a reversed list. If
x.reverse() does the former, a disadvantage relative to the procedural
approach is that a function can be used in an expression. It is clearer
and more concise to write
z = reverse(x) + reverse(y)
than
x.reverse()
y.reverse()
z = x + y
Furthermore, if in Python the algorithm for the reverse function
applies to many kinds of objects, it just needs to be coded once,
whereas a reverse method would have to provided for each class that
uses it (perhaps through inheritance).
More information about the Python-list
mailing list