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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:44:31+00:00 2026-05-24T09:44:31+00:00

I started with this code snippet, which, by my understanding, is essentially a class-factory

  • 0

I started with this code snippet, which, by my understanding, is essentially a class-factory of sorts to emulate an “enum” type from other languages:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)

(which I took from here: How can I represent an 'Enum' in Python? )

I get how it works, and it does, but I would like to make my dynamically generated classes, which are of type ‘Enum’, iterable so that i can do the following in order to do a sanity check:

MyEnum = enum('FOO', 'BAR', 'JIMMY')
def func(my_enum_value):  # expects one of the MyEnum values
    if not my_enum_value in MyEnum:
        raise SomeSortOfException

However, in order to make that sanity check work, I need to make MyEnum iterable. I read here: http://pydanny.blogspot.com/2007/10/required-methods-to-make-class-iterable.html that I needed to add iter len contains and getitem methods to something in order to make it iterable. I started down this road (rewriting the enum code) but got stuck:

def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    enums['_enums'] = enums.copy() # (so I'd have them in the Class in order to use for the methods I'll be implementing to make it iterable)
    Enum = type('Enum', (), enums)

    Enum.__iter__ = #Er, how do I do this? Guess I'll ask SO
    Enum.__len__ =  # etc. 
    # . . . 
    return Enum

SO, how do I make my generated Enum classes iterable so I can use the ‘in’ word with them? This isn’t life or death for me, but I’m learning a ton of Python by doing this.

  • 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-24T09:44:34+00:00Added an answer on May 24, 2026 at 9:44 am

    Use a smarter metaclass than type.

    class EnumMC(type):
      def __contains__(self, val):
        return val in self.__dict__
    
    def enum(*sequential, **named):
      enums = dict(zip(sequential, range(len(sequential))), **named)
      return EnumMC('Enum', (), enums)
    
    MyEnum = enum('FOO', 'BAR', 'JIMMY')
    def func(my_enum_value):  # expects one of the MyEnum values
        if not my_enum_value in MyEnum:
            raise ValueError()
    
    func('FOO')
    func('QUUX')
    

    You may want to use an actual attribute to store the enum keys rather than depending on the class dictionary though.

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

Sidebar

Related Questions

No related questions found

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.