What is the equivalent of passing functions as arguments using an object oriented approach

183 Views Asked by At

I have a program in python that includes a class that takes a function as an argument to the __init__ method. This function is stored as an attribute and used in various places within the class. The functions passed in can be quite varied, and passing in a key and then selecting from a set of predefined functions would not give the same degree of flexibility.

Now, apologies if a long list of questions like this is not cool, but...

  • Is their a standard way to achieve this in a language where functions aren't first class objects?
  • Do blocks, like in smalltalk or objective-C, count as functions in this respect?
  • Would blocks be the best way to do this in those languages?
  • What if there are no blocks?
  • Could you add a new method at runtime?
  • In which languages would this be possible (and easy)?
  • Or would it be better to create an object with a single method that performs the desired operation?
  • What if I wanted to pass lots of functions, would I create lots of singleton objects?
  • Would this be considered a more object oriented approach?
  • Would anyone consider doing this in python, where functions are first class objects?
4

There are 4 best solutions below

8
On BEST ANSWER

I don't understand what you mean by "equivalent... using an object oriented approach". In Python, since functions are (as you say) first-class objects, how is it not "object-oriented" to pass functions as arguments?

a standard way to achieve this in a language where functions aren't first class objects?

Only to the extent that there is a standard way of functions failing to be first-class objects, I would say.

In C++, it is common to create another class, often called a functor or functionoid, which defines an overload for operator(), allowing instances to be used like functions syntactically. However, it's also often possible to get by with plain old function-pointers. Neither the pointer nor the pointed-at function is a first-class object, but the interface is rich enough.

This meshes well with "ad-hoc polymorphism" achieved through templates; you can write functions that don't actually care whether you pass an instance of a class or a function pointer.

Similarly, in Python, you can make objects register as callable by defining a __call__ method for the class.

Do blocks, like in smalltalk or objective-C, count as functions in this respect?

I would say they do. At least as much as lambdas count as functions in Python, and actually more so because they aren't crippled the way Python's lambdas are.

Would blocks be the best way to do this in those languages?

It depends on what you need.

Could you add a new method at runtime? In which languages would this be possible (and easy)?

Languages that offer introspection and runtime access to their own compiler. Python qualifies.

However, there is nothing about the problem, as presented so far, which suggests a need to jump through such hoops. Of course, some languages have more required boilerplate than others for a new class.

Or would it be better to create an object with a single method that performs the desired operation?

That is pretty standard.

What if I wanted to pass lots of functions, would I create lots of singleton objects?

You say this as if you might somehow accidentally create more than one instance of the class if you don't write tons of boilerplate in an attempt to prevent yourself from doing so.

Would this be considered a more object oriented approach?

Again, I can't fathom your understanding of the term "object-oriented". It doesn't mean "creating lots of objects".

Would anyone consider doing this in python, where functions are first class objects?

Not without a need for the extra things that a class can do and a function can't. With duck typing, why on earth would you bother?

5
On

I'm just going to answer some of your questions.

As they say in the Scheme community, "objects are a poor man's closures" (closures being first-class functions). Blocks are usually just syntactic sugar for closures. For languages that do not have closures, there exist various solutions.

One of the common solutions is to use operator overloading: C++ has a notion of function objects, which define a member operator() ("operator function call"). Python has a similar overloading mechanism, where you define __call__:

class Greeter(object):
    def __init__(self, who):
         self.who = who

    def __call__(self):
         print("Hello, %s!" % who)

hello = Greeter("world")
hello()

Yes, you might consider using this in Python instead of storing functions in objects, since functions can't be pickled.

In languages without operator overloading, you'll see things like Guava's Function interface.

3
On

You could use the strategy pattern. Basically you pass in an object with a known interface, but different behavior. It's like passing function but one that's wrapped up in an object.

0
On

In Smalltalk you'd mostly be using blocks. You can also create classes and instances at runtime.