Python, Ruby and OOPs

I have found quite some blogs and articles mentioning python as not being completly object oriented and ruby as a better alternative in this perspective. Of course all entities in python are objects. But instead of providing methods to objects to solve all problems, python provides a mixture of builtin functions, module functions and object methods. I like python's approach and feel that is the right one. Can all problems be modeled and solved elegantly using object and methods ? I don't think so.

Following is from an interview with Alexander Stepanov the creator of C++ STL where he mentions.

I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with  axioms. You  do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work.

Operations in STL  algorithm are a good set of examples where independent functions makes sense over object methods. Some such examples in python and ruby where they take different approach are


zip
a built-in function provided by python which takes a iterables and returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
zip([1,2], [3,4], [4,5]) = [(1, 3, 4), (2, 4, 5)]

product
a module(itertools) function which does a cartesian product of input sequences.
list(product([1,2], [2,3], [4,5])) = [(1,2,4), (1,2,5), (1,3,4), (1,3,5), (2,2,4), (2,2,5), (2,3,4), (2,3,5)]

Ruby provides zip and product as a methods of list through Enumerable mixin.
[1,2].zip([3,4], [4,5]) = [(1, 3, 4), (2, 4, 5)]
[1,2].product([3,4],[5,6]) = [[1,3,5],[1,3,6],[1,4,5],[1,4,6],  [2,3,5],[2,3,6],[2,4,5],[2,4,6]]

Ultimately both solves the problem. But what is the meaning of [1,2].zip([3,4], [4,5]) or [1,2].product([3,4],[5,6]) ? product is defined as the cartesian product of a set of items where no item has a special meaning. But with oops representation the first item is special and has a different meaning. It is providing a functionality to combine others.

Comments

Popular posts from this blog

select, poll and epoll, a twisted story

Python and anonymous blocks

Python, json and garbage collection