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 173483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:21:08+00:00 2026-05-11T13:21:08+00:00

Is it possible to pass a method as a parameter to a method? self.method2(self.method1)

  • 0

Is it possible to pass a method as a parameter to a method?

self.method2(self.method1)  def method1(self):     return 'hello world'  def method2(self, methodToRun):     result = methodToRun.call()     return result 
  • 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. 2026-05-11T13:21:09+00:00Added an answer on May 11, 2026 at 1:21 pm

    Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.

    Since you asked about methods, I’m using methods in the following examples, but note that everything below applies identically to functions (except without the self parameter).

    To call a passed method or function, you just use the name it’s bound to in the same way you would use the method’s (or function’s) regular name:

    def method1(self):     return 'hello world'  def method2(self, methodToRun):     result = methodToRun()     return result  obj.method2(obj.method1) 

    Note: I believe a __call__() method does exist, i.e. you could technically do methodToRun.__call__(), but you probably should never do so explicitly. __call__() is meant to be implemented, not to be invoked from your own code.

    If you wanted method1 to be called with arguments, then things get a little bit more complicated. method2 has to be written with a bit of information about how to pass arguments to method1, and it needs to get values for those arguments from somewhere. For instance, if method1 is supposed to take one argument:

    def method1(self, spam):     return 'hello ' + str(spam) 

    then you could write method2 to call it with one argument that gets passed in:

    def method2(self, methodToRun, spam_value):     return methodToRun(spam_value) 

    or with an argument that it computes itself:

    def method2(self, methodToRun):     spam_value = compute_some_value()     return methodToRun(spam_value) 

    You can expand this to other combinations of values passed in and values computed, like

    def method1(self, spam, ham):     return 'hello ' + str(spam) + ' and ' + str(ham)  def method2(self, methodToRun, ham_value):     spam_value = compute_some_value()     return methodToRun(spam_value, ham_value) 

    or even with keyword arguments

    def method2(self, methodToRun, ham_value):     spam_value = compute_some_value()     return methodToRun(spam_value, ham=ham_value) 

    If you don’t know, when writing method2, what arguments methodToRun is going to take, you can also use argument unpacking to call it in a generic way:

    def method1(self, spam, ham):     return 'hello ' + str(spam) + ' and ' + str(ham)  def method2(self, methodToRun, positional_arguments, keyword_arguments):     return methodToRun(*positional_arguments, **keyword_arguments)  obj.method2(obj.method1, ['spam'], {'ham': 'ham'}) 

    In this case positional_arguments needs to be a list or tuple or similar, and keyword_arguments is a dict or similar. In method2 you can modify positional_arguments and keyword_arguments (e.g. to add or remove certain arguments or change the values) before you call method1.

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

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If they're all on the same domain, then why treat… May 12, 2026 at 11:01 pm
  • Editorial Team
    Editorial Team added an answer This sounds similar to what Facebook Platform applications do. One… May 12, 2026 at 11:01 pm
  • Editorial Team
    Editorial Team added an answer Check out this paper (PDF link) by Richard Fateman. The… May 12, 2026 at 11:01 pm

Related Questions

I have been lead to believe that it is possible to pass a class
Is it possible to pass a list constructor parameter when resolving a type? I
In C or C++ it is possible to use a function pointer as a
I realize the title needs to be read more than once for understanding ...

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.