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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:49:32+00:00 2026-06-15T18:49:32+00:00

I only just started learning Python and found out that I can pass a

  • 0

I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar()) it will not pass as a function pointer but the return value of the used function. Calling foo(bar) will pass the function, but this way I am not able to pass any additional arguments. What if I want to pass a function pointer that calls bar(42)?

I want the ability to repeat a function regardless of what arguments I have passed to it.

def repeat(function, times):
    for calls in range(times):
        function()

def foo(s):
        print s

repeat(foo("test"), 4)

In this case the function foo("test") is supposed to be called 4 times in a row.
Is there a way to accomplish this without having to pass “test” to repeat instead of foo?

  • 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-06-15T18:49:33+00:00Added an answer on June 15, 2026 at 6:49 pm

    You can either use a lambda:

    repeat(lambda: bar(42))
    

    Or functools.partial:

    from functools import partial
    repeat(partial(bar, 42))
    

    Or pass the arguments separately:

    def repeat(times, f, *args):
        for _ in range(times):
            f(*args)
    

    This final style is quite common in the standard library and major Python tools. *args denotes a variable number of arguments, so you can use this function as

    repeat(4, foo, "test")
    

    or

    def inquisition(weapon1, weapon2, weapon3):
        print("Our weapons are {}, {} and {}".format(weapon1, weapon2, weapon3))
    
    repeat(10, inquisition, "surprise", "fear", "ruthless efficiency")
    

    Note that I put the number of repetitions up front for convenience. It can’t be the last argument if you want to use the *args construct.

    (For completeness, you could add keyword arguments as well with **kwargs.)

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

Sidebar

Related Questions

I'm hoping you guys can answer me a question? I've only just started out
I just started out with jQuery and really only need it for some very
I just started writing powershell scripts for Citrix XenServer. However, I can only find
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I've only just started learning to build iPhone apps using xcode. I'm slowly getting
I have just started learning Scala and I'm now wondering how I could implement
I've only just started learning ruby on rails and I would like to create
$('.button').click(function(){ $(this).toggleClass('clicked') $('.clicked+span').toggleClass('choice'); }); I've just started learning jQuery and I was playing around
I just started learning haskell, and I tried to implement a simple function to
I've just started learning C++, and to display the outputs of code I found

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.