Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43506

Re: Functional vs. Object oriented API

From Rui Maciel <rui.maciel@gmail.com>
Newsgroups comp.lang.python
Subject Re: Functional vs. Object oriented API
Date 2013-04-13 09:51 +0100
Organization A noiseless patient Spider
Message-ID <kkb65c$66o$1@dont-email.me> (permalink)
References <mailman.429.1365635789.3114.python-list@python.org>

Show all headers | View raw


Max Bucknell wrote:

> Hi,
> I'm currently learning Python, and it's going great. I've dabbled before,
> but really getting into it is good fun.
> 
> To test myself, and not detract too much from my actual studies
> (mathematics), I've been writing my own package to do linear algebra, and
> I am unsure about how best to structure my API.
> 
> For example, I have a vector class, that works like so:
> 
>     >>> a = Vector([2, 7, 4])
>     >>> b = Vector.j # unit vector in 3D y direction
> 
> I also have a function to generate the dot product of these two vectors.
> In Java, such a function would be put as a method on the class and I would
> do something like:
> 
>     >>> a.dot_product(b)
>     7

Not necessarily.  That would only happen if that code was designed that way.  
It's quite possible, and desirable, that the dot product isn't implemented 
as a member function of the vector data type, and instead is implemented as 
an operator to be applied to two object.


> and that would be the end of it. But in Python, I can also have:
> 
>     >>> dot_product(a, b)
>     7
> 
> Which of these two are preferred in Python? And are there any general
> guidelines for choosing between the two styles, or is it largely a matter
> of personal preference?

The separation of concerns principle is a good guideline.  This doesn't 
apply exclusively to Python; it essentiallyl applies to all programming 
languages.

http://en.wikipedia.org/wiki/Separation_of_concerns


There are significant advantages in separating the definition of a data type 
from the definition of the operations that are to be applied to it.  If 
operations are decoupled from the data type then it's possible to preserve 
the definition of that data type eternally, while the operators that are 
written to operate on it can be added, tweaked and removed independently and 
at anyone's whims.


Hope this helps,
Rui Maciel

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Functional vs. Object oriented API Max Bucknell <mpwb500@york.ac.uk> - 2013-04-11 00:16 +0100
  Re: Functional vs. Object oriented API Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-12 04:20 +0000
    Re: Functional vs. Object oriented API Roy Smith <roy@panix.com> - 2013-04-12 10:19 -0400
      Re: Functional vs. Object oriented API Mitya Sirenef <msirenef@lightbird.net> - 2013-04-12 10:29 -0400
      Re: Functional vs. Object oriented API David M Chess <chess@us.ibm.com> - 2013-04-12 11:37 -0400
        Re: Functional vs. Object oriented API 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-12 22:25 -0700
        Re: Functional vs. Object oriented API 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-12 22:25 -0700
  Re: Functional vs. Object oriented API Rui Maciel <rui.maciel@gmail.com> - 2013-04-13 09:51 +0100

csiph-web