2

I'm writing a small piece of code in Python and am curious what other people think of this.

I have a few classes, each with a few methods, and am trying to determine what is "better": to pass objects through method calls, or to pass methods through method calls when only one method from an object is needed. Basically, should I do this:

def do_something(self, x, y, manipulator):
    self.my_value = manipulator.process(x, y)

or this

def do_the_same_thing_but_differently(self, x, y, manipulation):
    self.my_value = manipulation(x, y)

The way I see it, the second one is arguably "better" because it promotes even looser coupling/stronger cohesion between the manipulation and the other class. I'm curious to see some arguments for and against this approach for cases when only a single method is needed from an object.

EDIT: I removed the OOP wording because it was clearly upsetting. I was mostly referring to loose coupling and high cohesion.

Corey D
  • 4,689
  • 4
  • 25
  • 33
  • Stronger OOP? What are you measuring for the "strength" of OOP? I've never seen this before. Do you have a quote or a link? – S.Lott Jan 09 '11 at 19:28
  • http://en.wikipedia.org/wiki/Loose_coupling – Corey D Jan 09 '11 at 19:32
  • 2
    What's "better" depends on what you're trying to achieve. If the function is only interested in calling that method, passing that method gives you greater flexibility (you can give it any callable). On the other hand, if it wants to do anything else with the `manipulator` object, or if you want to be sure you're only calling a particular method, you have to use the first method. – Thomas K Jan 09 '11 at 19:41
  • That's coupling, not OO. I'm curious about your use of the "Stronger OOP" phrase. I'm not curious about "strong coupling" and "loose coupling". – S.Lott Jan 09 '11 at 19:53
  • "clearly upsetting"? Perhaps you mean "confusing". – S.Lott Jan 10 '11 at 11:44

3 Answers3

4

The second solution may provide looser coupling because it is more "functional", not more "OOP". The first solution has the advantage that it works in languages like C++ which don't have closures (though one can get a similar effect using templates and pointer-to-member-functions); but in a language like Python, IMHO the 2nd alternative seems to be more "natural".

EDIT: you will find a very nice discussion of "functional vs. object oriented" techniques in the free book "Higher order Perl", available here:

http://hop.perl.plover.com/

(look into chapter 1, part 6). Though it is a Perl (and not a Python) book, the discussion there fits exactly to the question asked here, and the functional techniques described there can be applied to Python in a similar way.

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
  • Agreed. If the sole purpose of "manipulator" is to provide a "process" function, it is definitely more Pythonic to pass the function itself rather than an object. – AndreiM Jan 09 '11 at 19:41
  • Here's the question. Does "more functional" meet the "stronger OOP" criteria? Or more functional mean "weaker OOP"? I think the question -- as written -- is really hard to answer directly. – S.Lott Jan 09 '11 at 19:54
  • IMHO "Functional" and "object-oriented are mostly orthogonal paradigms. So "more functional" does not imply "more or less OOP" and vice versa. – Doc Brown Jan 09 '11 at 20:00
  • @Doc Brown: "So "more functional" does **not** imply..." an infinite number of things. What does it imply? Does "more functional" meet the "stronger OOP" criteria? Or does "more functional" mean "weaker OOP"? – S.Lott Jan 10 '11 at 02:29
  • @S. Lott: sorry, I am not interested in philosophical discussions. Perhaps you try a look into the first chapter of the free book which I gave the link to above and form you own point of view. – Doc Brown Jan 10 '11 at 06:04
  • @Doc Brown: I'm trying to understand your comments. You said that "Functional" and "object-oriented" are mostly orthogonal paradigms. I can parse that. It sounds like more functional is less object oriented. Then you said other things which I can't parse. I'm trying to understand all of what you said. Can you give a simple clarification to the question does "more functional" meet the "stronger OOP" criteria? I can't parse some of the things you said. Please clarify your comments. My point of view is not the issue. Your comments are what I want to understand. – S.Lott Jan 10 '11 at 11:48
  • @S. Lott: "orthogonal" is another word for "almost independent" (as a metaphor, I had a diagram in mind with a coordinate system where one axis is "OOP vs. non-OOP" and another axis is "functional vs. imperative"). Perhaps there's just a simple misunderstanding of that word? – Doc Brown Jan 10 '11 at 13:17
1

I will say the second approach ; because it's definitely look like a callback which they are very used when using the Hollywood principle (don't call us we will call you) which is a paradigm that assists in the development of code with high cohesion and low coupling [Ref 2] .

mouad
  • 67,571
  • 18
  • 114
  • 106
0

I would definitely go with the second approach.

Also consider that you could change the interface of whatever Manipulator class so that process is instead spelled __call__, and then it will work transparently with the second approach.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153