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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:40:30+00:00 2026-06-13T08:40:30+00:00

I have a Python function called func that I am trying to time using

  • 0

I have a Python function called func that I am trying to time using timeit.

func takes an argument, f, that is also a function. In the program I’m writing, it’s a lambda function, but it can be a regularly defined function as well. What matters is that the source code for this function is not available to func, all that’s available is a reference to it.

What I’m trying to do:

def testTimingOfFunc(f):
    time = timeit.timeit("package.func(f)", "from src import package")

I get an error that says that f isn’t defined.

Everything else is working correctly. When I call:

testTimingOfFunc('lambda x:x**2')

and change the function to:

def testTimingOfFunc(f):
    time = timeit.timeit("package.func(" + str(f) + ")",
                         "from src import package")

everything works as it should, so I know that everything regarding the timeit function is fine.

That’s not how testTimingOfFunc works though; A function reference gets passed in, not a string.

Any ideas on how I can pass f as an argument to func in timeit?

  • 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-13T08:40:32+00:00Added an answer on June 13, 2026 at 8:40 am

    timeit can be called with a python callable value: just change your method to:

    from src import package
    import functools
    
    def testTimingOfFunc(f):
        time = timeit.timeit(functools.partial(package.func, f))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with datetime objects in python. I have a function that takes
I have a python function that gets an array called row. Typically row contains
I have a working python 2.7 program that calls a DLL. I am trying
I have a python function that makes a subprocess call to a shell script
I have a python function that randomize a dictionary representing a position specific scoring
I have a python program which calls a C function. I send some structures
we are trying to create a calendar function in python. we have created a
I have a python script that I run with 'exec'. When a function is
I have an expensive function that takes and returns a small amount of data
I have a python function that contains an if-statement to check something. def my_func(a,

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.