473,624 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

functional or object-oriented?

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?

Sep 19 '05 #1
8 1560
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."
Sep 19 '05 #2
> 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.


Which is not what functional programming is about. The style you
describe is more often referred to as procedural programming.

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

See

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

for what functional programming is about.

Diez
Sep 19 '05 #3
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.

Sep 19 '05 #4
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
;)

Sep 20 '05 #5
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.

Sep 20 '05 #6
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 'o****@xiludom. gro'.split('@')])"
Sep 20 '05 #7
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.
Sep 20 '05 #8
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.

Sep 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3432
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
1
2812
by: mcstock | last post by:
before i begin to roll my own, can anybody recommend a web-based application design & tracking tool that provides: functional hierarchy/network definition work break-down structures module definition & mapping to functional hierarchy issue tracking and resolution version and configuration management -- Mark C. Stock
0
2255
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of images: /Users/t/t4/oh/DSCN2059m-s.jpg
23
3615
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a so-called pre-fix notation or algebraic notation. Algebraic notations have the concept of operators, meaning, symbols placed around arguments. In algebraic in-fix notation, different
60
4902
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
1
1547
by: petermichaux | last post by:
Hi, I have a general JavaScript library API design question based on some examples I have seen. There seem to be two options The first option is used by the AJAX libraries and by Matt Kruse's AJAX library. These seem like functional programming. YAHOO.util.connect.asyncRequest('GET', '/the/url', {success:function(o){alert(responseText)};});
3
303
by: hn.ft.pris | last post by:
I've got following code test C++'s functor. For the sake of easy-reading, I omit some declearations. #include <algorithm> #include <functional> using namespace std;
30
5458
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
15
1719
by: Lorenzo Stella | last post by:
Hi all, I haven't experienced functional programming very much, but now I'm trying to learn Haskell and I've learned that: 1) in functional programming LISTS are fundmental; 2) any "cycle" in FP become recursion. I also know that Python got some useful tool such as map, filter, reduce... so I told: "let's try some FP-style programming with Python!". I took a little example of Haskell: listprimes :: Integer -
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.