Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:21+00:00 2026-05-26T15:31:21+00:00

I have a program in python that includes a class that takes a function

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:31:21+00:00Added an answer on May 26, 2026 at 3:31 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python program (with Django - does this matter?) that I want
I have a python program that does something like this: Read a row from
I have a python program that uses a custom-built DLL. This DLL crashes due
I have a Python program that uses the threading module. Once every second, my
I have a python program that reads floating point values using the following regular
I have a python program that opens several urls in seperate tabs in a
I have a python program that is going to eat a lot of memory,
I have a python program/file that I want to run repeatedly and calculate the
I have a simple python program that I'd like to daemonize. Since the point
I have a Python script that needs to execute an external program, but for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.