Functional vs. Object oriented API

Max Bucknell mpwb500 at york.ac.uk
Wed Apr 10 19:16:19 EDT 2013


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

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?

Thanks for reading,
Max.


More information about the Python-list mailing list