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

The Archive Base Latest Questions

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

What is the most elegant and pythonic way to turn a test such as:

  • 0

What is the most elegant and pythonic way to turn a test such as:

def is_even(n):
  return n % 2 == 0

Into a logically equivalent (but different syntactically) test such as:

if n in even_numbers:
  # assert is_even(n)
  # ...

I can think of a few ways to create the object even_numbers, but I’m interested to hear if there are nice ways I might have overlooked.


Note: I’m aware this probably sound like an XY problem, so my use case is concerning the choices kwarg to argparse.ArgumentParser.add_argument, and whether it’s going to be nicer to use that or alternatively to munge the callable type kwarg.

  • 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:22:53+00:00Added an answer on June 15, 2026 at 6:22 pm

    I would turn even_numbers into a container type:

    class EvenNumbers(object):
    
        # Add other container methods as needed
    
        def __contains__(self, value):
            return value % 2 == 0
    
    even_numbers = EvenNumbers()
    

    Then,

    >>> 3 in even_numbers
    False
    >>> 4 in even_numbers
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: most elegant way to return a string from List<int> I'm not sure
What is the most elegant way to return a string from a List ok,
What would be the most elegant way to turn any array of (equal length)
What´s the most efficient, elegant and pythonic way of solving this problem? Given a
What's the most elegant way to return a std::list object from a shared lib
Do you have any insights into the most elegant way of persisting objects from
What is the easiest/most elegant way to do the following in python: def piecewiseProperty(aList):
What is the most elegant way to identify an error into transaction? At this
Assuming connectionDetails is a Python dictionary, what's the best, most elegant, most pythonic way
What's the most elegant way to collapse duplicate columns from tabular data into lists?

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.