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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:02:25+00:00 2026-06-04T21:02:25+00:00

I am wondering if there is a way to determine (given a variable containing

  • 0

I am wondering if there is a way to determine (given a variable containing a lambda) the number of parameters the lambda it contains. The reason being, I wish to call a function conditionally dependent on the number of parameters.

What I’m looking for

def magic_lambda_parameter_counting_function(lambda_function):
    """Returns the number of parameters in lambda_function

    Args:
        lambda_function - A lambda of unknown number of parameters
    """

So I can do something like

def my_method(lambda_function):

    # ... 
    # (say I have variables i and element)

    parameter_count = magic_lambda_parameter_counting_function(lambda_function)

    if parameter_count == 1:
        lambda_function(i)
    elif parameter_count == 2:
        lambda_function(i, element)
  • 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-04T21:02:27+00:00Added an answer on June 4, 2026 at 9:02 pm

    I’m skipping the part about how to count the arguments, because I don’t know how you want to consider varargs and keywords. But this should get you started.

    >>> import inspect
    >>> foo = lambda x, y, z: x + y + z
    >>> inspect.getargspec(foo)
    ArgSpec(args=['x', 'y', 'z'], varargs=None, keywords=None, defaults=None)
    

    It sounds like inspect.getargspec is deprecated as of Python 3 (thanks to @JeffHeaton). The recommend solution uses inspect.signature. The .parameters member of the result contains a variety of structures depending on the arrangement of parameters to the function in question, but I’m matching my function from the Python 2 example above.

    >>> import inspect
    >>> foo = lambda x, y, z: x + y + z
    >>> inspect.signature(foo).parameters
    mappingproxy(OrderedDict([('x', <Parameter "x">), ('y', <Parameter "y">), ('z', <Parameter "z">)]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if there is any way to get the lambda expressions that result from
I was wondering if there was a way within an app code to determine
I'm wondering if there's a way to determine which version of Linux I'm running
I was wondering if there was a way to determine if the last column
Is there an easy way to determine the number of digits a GMP integer
Is there a way for me to determine the total number of Twitter messages
I was wondering if there is a way to determine if an image is
I was wondering if there was a way to determine the height/width of a
I was wondering is there a way in PHP that you could tell where
I am wondering is there a way to render a partial view in the

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.