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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:33:34+00:00 2026-05-15T06:33:34+00:00

I’m curious if it’s possible to take several conditional functions and create one function

  • 0

I’m curious if it’s possible to take several conditional functions and create one function that checks them all (e.g. the way a generator takes a procedure for iterating through a series and creates an iterator).

The basic usage case would be when you have a large number of conditional parameters (e.g. “max_a”, “min_a”, “max_b”, “min_b”, etc.), many of which could be blank. They would all be passed to this “function creating” function, which would then return one function that checked them all. Below is an example of a naive way of doing what I’m asking:

def combining_function(max_a, min_a, max_b, min_b, ...):
    f_array = []
    if max_a is not None:
        f_array.append( lambda x: x.a < max_a )
    if min_a is not None:
        f_array.append( lambda x: x.a > min_a )
    ...

    return lambda x: all( [ f(x) for f in f_array ] )

What I’m wondering is what is the most efficient to achieve what’s being done above? It seems like executing a function call for every function in f_array would create a decent amount of overhead, but perhaps I’m engaging in premature/unnecessary optimization. Regardless, I’d be interested to see if anyone else has come across usage cases like this and how they proceeded.

Also, if this isn’t possible in Python, is it possible in other (perhaps more functional) languages?

EDIT: It looks like the consensus solution is to compose a string containing the full collection of conditions and then use exec or eval to generate a single function. @doublep suggests this is pretty hackish. Any thoughts on how bad this is? Is it plausible to check the arguments closely enough when composing the function that a solution like this could be considered safe? After all, whatever rigorous checking is required only needs to be performed once whereas the benefit from a faster combined conditional can be accrued over a large number of calls. Are people using stuff like this in deployment scenarios or is this mainly a technique to play around with?

  • 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-15T06:33:35+00:00Added an answer on May 15, 2026 at 6:33 am

    Replacing

    return lambda x: all( [ f(x) for f in f_array ] )
    

    with

    return lambda x: all( f(x) for f in f_array )
    

    will give a more efficient lambda as it will stop early if any f returns a false value and doesn’t need to create unnecessary list. This is only possible on Python 2.4 or 2.5 and up, though. If you need to support ancient values, do the following:

    def check (x):
        for f in f_array:
            if not f (x):
                return False
        return True
    
    return check
    

    Finally, if you really need to make this very efficient and are not afraid of bounding-on-hackish solutions, you could try compilation at runtime:

    def combining_function (max_a, min_a):
        constants = { }
        checks    = []
    
        if max_a is not None:
            constants['max_a'] = max_a
            checks.append ('x.a < max_a')
    
        if min_a is not None:
            constants['min_a'] = min_a
            checks.append ('x.a > min_a')
    
        if not checks:
            return lambda x: True
        else:
            func = 'def check (x): return (%s)' % ') and ('.join (checks)
            exec func in constants, constants
            return constants['check']
    
    class X:
        def __init__(self, a):
            self.a = a
    
    check = combining_function (3, 1)
    print check (X (0)), check (X (2)), check (X (4))
    

    Note that in Python 3.x exec becomes a function, so the above code is not portable.

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

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • 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 Setting ShowsNavigationUI=False on a Page ought to do it. There… May 15, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer You don't need a loop as youre dealing with a… May 15, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer Is this something that is sometimes done? Yes, views are… May 15, 2026 at 9:09 am

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.