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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:36:56+00:00 2026-06-13T02:36:56+00:00

I am trying to create an enumeration in python. I have seen seen several

  • 0

I am trying to create an enumeration in python. I have seen seen several solutions (The second answer here by @alec thomas intrigued me most), but I would like to make the enumeration immutable. I found a python recipe that is immutable, but I want to have a dict-like key/value association.

I was attempting to use duck-punching to add properties to the class that would throw an AttributeError if you tried to call fset or fdel on the property.

I ran into trouble defining the fget function of the property. Here’s the code i have so far:

def enum(*sequential, **named):
    # Build property dict
    enums = dict(zip(sequential, range(len(sequential))), **named)

    # Define an errorhandler function
    def err_func(*args, **kwargs):
        raise AttributeError('Enumeration is immutable!')

    # Create a base type
    t = type('enum', (object,), {})

    # Add properties to class by duck-punching
    for attr, val in enums.iteritems():
        setattr(t, attr, property(lambda attr: enums[attr], err_func, err_func))

    # Return an instance of the new class
    return t()

e = enum('OK', 'CANCEL', 'QUIT')
print e
print e.OK
print e.CANCEL
print e.QUIT

# Immutable?
e.OK = 'ASDF'  # Does throw the correct exception
print e.OK

The output from this is:

<__main__.enum object at 0x01FC8F70>
Traceback (most recent call last):
  File "enum.py", line 24, in <module>
    print e.OK
  File "enum.py", line 17, in <lambda>
    setattr(t, attr, property(lambda attr: enums[attr], err_func, err_func))
KeyError: <__main__.enum object at 0x01FC8F70>

Perhaps this is not the best way to create an enumeration, but it’s short and I wanted to explore more of this whole duck-punching/monkey-patching concept.

  • 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-13T02:36:57+00:00Added an answer on June 13, 2026 at 2:36 am

    Your immediate problem is that the getter of a property takes self as the only parameter, not attr. Therefore, you should use something like lambda self: val instead.

    However, that doesn’t work because the lambda binds the name val, which changes from iteration to iteration. So you will need to wrap it somehow:

    def getter(val):
        return lambda self: val
    
    for attr, val in enums.iteritems():
        setattr(t, attr, property(getter(val), err_func, err_func))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a enumeration class in objective-c. Here below is what I
Ok so I am trying create a login script, here I am using PHP5
Trying a simple project to create my own (very basic) data binding. I have
I have an enumeration that I am trying to initialize from a long value
I am trying to create a custom enumeration via a series of facts. greater(X,Y)
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Trying to create a black line in my view to separate text blocks but
Trying to create a facebook app just to learn and coming across a strange
Trying to create a new Dedicated Cache Role in Windows Azure but get the

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.