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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:34:21+00:00 2026-06-03T08:34:21+00:00

I need to simulate enums in Python, and did it by writing classes like:

  • 0

I need to simulate enums in Python, and did it by writing classes like:

class Spam(Enum):
    k = 3
    EGGS = 0
    HAM = 1
    BAKEDBEANS = 2

Now I’d like to test if some constant is a valid choice for a particular Enum-derived class, with the following syntax:

if (x in Foo):
    print("seems legit")

Therefore I tried to create an “Enum” base class where I override the __contains__ method like this:

class Enum:
    """
    Simulates an enum.
    """

    k = 0 # overwrite in subclass with number of constants

    @classmethod
    def __contains__(cls, x):
        """
        Test for valid enum constant x:
            x in Enum
        """
        return (x in range(cls.k))

However, when using the in keyword on the class (like the example above), I get the error:

TypeError: argument of type 'type' is not iterable

Why that? Can I somehow get the syntactic sugar I want?

  • 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-03T08:34:23+00:00Added an answer on June 3, 2026 at 8:34 am

    Why that?

    When you use special syntax like a in Foo, the __contains__ method is looked up on the type of Foo. However, your __contains__ implementation exists on Foo itself, not its type. Foo‘s type is type, which doesn’t implement this (or iteration), thus the error.

    The same situation occurs if you instantiate an object and then, after it is created, add a __contains__ function to the instance variables. That function won’t be called:

    >>> class Empty: pass
    ... 
    >>> x = Empty()
    >>> x.__contains__ = lambda: True
    >>> 1 in x
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: argument of type 'Empty' is not iterable
    

    Can I somehow get the syntactic sugar I want?

    Yes. As mentioned above, the method is looked up on Foo‘s type. The type of a class is called a metaclass, so you need a new metaclass that implements __contains__.

    Try this one:

    class MetaEnum(type):
        def __contains__(cls, x):
                return x in range(cls.k)
    

    As you can see, the methods on a metaclass take the metaclass instance — the class — as their first argument. This should make sense. It’s very similar to a classmethod, except that the method lives on the metaclass and not the class.

    Inheritance from a class with a custom metaclass also inherits the metaclass, so you can create a base class like so:

    class BaseEnum(metaclass=MetaEnum):
        pass
    
    class MyEnum(BaseEnum):
        k = 3
    
    print(1 in MyEnum) # True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an app where I need to simulate key press events on a
I have a class derived from Dictionary. I need this class to simulate a
I'm working with visual python on a project where I need to simulate a
I'm writing an application in which I need to simulate a textarea. The only
I need to simulate a test scenario in which I call the getBytes() method
I need to simulate multiple embedded server devices that are typically used for motor
I need to simulate how my application will look when a user is driving
I need to simulate a memory-hungry process. For example, On a machine with 4.0
I'm looking for a jquery plugin to simulate a vertical marquee. I need it
I am working on a Java project and need to have a keypress simulate

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.