functional or object-oriented?

Discussion in 'Python' started by beza1e1, Sep 19, 2005.

  1. beza1e1

    beza1e1 Guest

    I see myself shifting more and more over to the functional kind of
    coding. Could be related to the Haskell, we had to learn in CS. Now i
    was wondering, how other people use Python?

    With functional i mean my files mostly consist of functions and only
    rarely i use "class". The library modules seem to be mostly written the
    object-way on the other hand.

    If you use both paradigms. What are your criterias to choose the right
    method for a project?
     
    beza1e1, Sep 19, 2005
    #1
    1. Advertising

  2. beza1e1 wrote:

    >I see myself shifting more and more over to the functional kind of
    >coding. Could be related to the Haskell, we had to learn in CS. Now i
    >was wondering, how other people use Python?
    >
    >With functional i mean my files mostly consist of functions and only
    >rarely i use "class". The library modules seem to be mostly written the
    >object-way on the other hand.
    >
    >If you use both paradigms. What are your criterias to choose the right
    >method for a project?
    >
    >
    >

    Here is a quote by Alex Martelli from the "Python Cookbook":

    "If the packaging is in terms of objects that typically comprise state
    and behavior, you're using OOP. Some object-oriented languages force
    you to use OOP for everything, so you end up with many object which lack
    either state or behavior. Python, however, supports multiple
    paradigms. While everything in Python is an object, you package things
    as OOP objects only when you want to. Other languages try to force your
    programming style into a predefined mold for your own good, while Python
    empowers you to make and express your own design choices.

    With OOP, once you have specified how an object is composed, you can
    instantiate as many objects of that kind as you need. When you don't
    want to create multiple objects, consider using other Python constructs
    such as modules."
     
    Steve Bergman, Sep 19, 2005
    #2
    1. Advertising

  3. Diez B. Roggisch, Sep 19, 2005
    #3
  4. beza1e1 a écrit :
    > I see myself shifting more and more over to the functional kind of
    > coding. Could be related to the Haskell, we had to learn in CS. Now i
    > was wondering, how other people use Python?
    >
    > With functional i mean my files mostly consist of functions


    which is not enough to make it 'functional'.

    > and only
    > rarely i use "class". The library modules seem to be mostly written the
    > object-way on the other hand.
    >
    > If you use both paradigms. What are your criterias to choose the right
    > method for a project?
    >


    Well, I'm most from an OO background, but I think OO and FP have in
    common to try to be as declarative as possible, FP by avoiding
    side-effects and relying on function composition, OO by hiding
    implementation and relying on polymorphic message dispatch. When it
    comes to 'pure' OO languages like Python, there's no real differences
    between classes, objects, functions, methods, attributes etc - they're
    *all* objects. So functional programming in Python is still OO ! When
    you realize that the def statement is nothing more than syntactic sugar
    to instantiate a function object, you can ask yourself if using or not
    using the class statement is really the question.

    Now to answer your question, I don't have 'criterias to choose the right
    method for a project'. I happily mix procedural, OO and FP wherever it
    fits.
     
    Bruno Desthuilliers, Sep 19, 2005
    #4
  5. beza1e1

    beza1e1 Guest

    You are right, this is not the essence of functional programming.

    Functional and procedural python code would look quite the same (at
    least in pydoc). It is the implementation of my functions, wether they
    are functional or procedural. If i use global variables, it is not
    functional any more.

    While python makes it use to work in a functional way, it is nearly
    impossible to do it exclusive. This is not necessary either, of course
    ;)
     
    beza1e1, Sep 20, 2005
    #5
  6. beza1e1

    beza1e1 Guest

    This nails it down, yes. :)

    I probably was too deep into OOP thinking-mode to work pythonic. So i
    am now rediscovering the python way.

    Have you read Paul Grahams On Lisp (or was it one of his essays)? He is
    strongly in favor of functional programming. Mainly because Lisp favors
    it. He does say though, simulations and CAD programs are inherently OO.
    But now i am writing a game modelling engine, i find objects are not
    the best way anytime in these fields either.
     
    beza1e1, Sep 20, 2005
    #6
  7. beza1e1 wrote:
    > This nails it down, yes. :)
    >
    > I probably was too deep into OOP thinking-mode to work pythonic. So i
    > am now rediscovering the python way.
    >
    > Have you read Paul Grahams On Lisp (or was it one of his essays)? He is
    > strongly in favor of functional programming.


    Yes, but this does not implies that FP is the main trend in CommonLisp.
    I discussed that point some years ago on c.l.lisp, and it turned out
    that Paul Grahams POV was not perceived by the communauty as
    representative of the most common usage of CommonLisp.

    > Mainly because Lisp favors
    > it.


    While being the father of FPLs, CommonLisp is not a 'pure' FPL, and
    clearly a multiparadigm language. BTW, it's object model is probably one
    of the most astonishing I've seen.

    > He does say though, simulations and CAD programs are inherently OO.
    > But now i am writing a game modelling engine, i find objects are not
    > the best way anytime in these fields either.


    Ok, but keep in mind you're using Python, not Lisp. While supporting
    some FP features (first class functions, nested functions, closures,
    list expressions, generators, and (a very restricted kind of) anonymous
    functions...), Python is still a 'pure' OOPL (ie : everything's an
    object). Not using the *class* statement when there's no use for it
    doesn't mean not using *objects*. One of the pre-requisites for FP is
    first-class functions, and Python provides this by defining functions as
    instances of class function. So even the most FP Python programs are
    still OO, at least under the hood !-)

    My 2 cents
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Sep 20, 2005
    #7
  8. beza1e1

    Tom Anderson Guest

    On Mon, 19 Sep 2005, beza1e1 wrote:

    > I see myself shifting more and more over to the functional kind of
    > coding. Could be related to the Haskell, we had to learn in CS. Now i
    > was wondering, how other people use Python?


    I'm a lot like you. I grew up with java, and learned to write classical
    object-oriented code. When i came over to python, i very quickly found
    myself writing more procedural, and in fact functional, code.

    I think this is a result of the kind of programs i'm writing. Objects are
    good when you have entities that will live a long and unpredictable life -
    chunks of text in a word processor, for example. If you're writing
    programs with simpler narratives, though, as i often am ("read in this
    data, parse it, transform it like so, shuffle it like this, then write it
    out like this"), a functional approach allows a simpler, cleaner factoring
    of the code.

    > With functional i mean my files mostly consist of functions and only
    > rarely i use "class". The library modules seem to be mostly written the
    > object-way on the other hand.


    The thing about OO code is that the pieces are self-contained, which makes
    this a good way to write library code. That's not a good explanation, but
    i haven't had any coffee this morning, so that's the best i can do right
    now.

    tom

    --
    Science runs with us, making us Gods.
     
    Tom Anderson, Sep 20, 2005
    #8
  9. beza1e1

    beza1e1 Guest

    I really should take a look at this CLOS, i think ... thanks for the
    background information.

    Do you think FP Python is appropriate or just syntactic sugar of a very
    sophisticated kind? Now i switching back to OO a bit, but the
    difference between data.value and date['value'] is not really in
    Pythons dynamic world.
     
    beza1e1, Sep 20, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Mayo
    Replies:
    168
    Views:
    3,490
    David Thompson
    Oct 22, 2007
  2. Replies:
    2
    Views:
    468
    Bruno Desthuilliers
    May 26, 2008
  3. Max Bucknell

    Functional vs. Object oriented API

    Max Bucknell, Apr 11, 2013, in forum: Python
    Replies:
    7
    Views:
    190
    Rui Maciel
    Apr 13, 2013
  4. Xavier Ho
    Replies:
    0
    Views:
    117
    Xavier Ho
    Apr 11, 2013
  5. Ian Kelly
    Replies:
    0
    Views:
    155
    Ian Kelly
    Apr 11, 2013
Loading...

Share This Page