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

  • Home
  • SEARCH
  • 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 6200139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:18:46+00:00 2026-05-24T04:18:46+00:00

I need to pass inequalities to a function for evaluation within the function. Is

  • 0

I need to pass inequalities to a function for evaluation within the function. Is there a way to evaluation the inequality if passed as a string? Or must I pass a representation of the inequality and use if/else statements to generate the sign?

  • 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-24T04:18:47+00:00Added an answer on May 24, 2026 at 4:18 am

    Your question is a little vague, but it sounds like you want to evaluate a string containing an expression (such as x > 5). Rather than doing that, which is unnecessarily complex, and potentially a security hazard, just define a function, either in the conventional way or using lambda.

    def gt5(x):
        return x > 5
    

    or

    gt5 = lambda x: x > 5
    

    These are both equivalent; now you can pass around gt5 however you like, and when the time comes, you simply call it

    y = 6
    if gt5(y):
        ...
    

    As Gerrat’s answer suggests, the operator module may also be useful for things like this.


    Now that I know you are processing user strings, I would definitely suggest creating a dictionary that maps strings to functions. (Perhaps that’s what you meant in your title?) Passing userland strings into getattr seems bad in a number of ways. What happens if you want to add a string that doesn’t correspond to an attribute of operator? What happens if the user passes in a string corresponding to a private attribute? Better to create a custom dictionary mapping strings to functions. A dict allows you to specify just those strings you want to accept.

    func_dict = {
        'lt'   : operator.lt,
        'gt'   : operator.gt,
        'nand' : lambda x, y: not (x and y)
    }
    

    You could still save yourself work by using getattr + operator to build the dict from a list of strings that you want to accept. Something like:

    func_dict = dict((s, getattr(operator, s)) for s in ['lt', 'gt'])
    

    Then update func_dict like so:

    custom_dict = {
        'nand' : lambda x, y: not (x and y)
    }
    
    func_dict.update(custom_dict)
    

    From there you can easily call functions in the dict like so:

    >>> func_dict['lt'](5, 7)
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass multiple arguments to a function that I would like to
I need to pass the $route to its inner function,but failed: function compilePath( $route
I need to pass a callback function that is CFuncType (ctypes.CFUNCTYPE or ctypes.PYFUNCTYPE...). How
I need to pass to function pointer to int. Now if I want to
I need to pass a string value from Form1 : public void button1_Click(object sender,
I need to pass a function as a parameter to another function and then
I need to pass 2 parameters in a query string but would like them
I need to pass two values to a javascript function from inside another javascript
I need to pass an ID and a password to a batch file at
I need to pass a regex substitution as a variable: sub proc { my

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.