Search code examples
pythonoopfirst-class-functions

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


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?

Solution

  • 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?