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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:41:18+00:00 2026-05-15T15:41:18+00:00

Source def flags(*opts): keys = [t[0] for t in opts] words = [t[1] for

  • 0

Source

def flags(*opts):
    keys = [t[0] for t in opts]
    words = [t[1] for t in opts]
    nums = [2**i for i in range(len(opts))]
    attrs = dict(zip(keys,nums))
    choices = iter(zip(nums,words))
    return type('Flags', (), dict(attrs))

Abilities = flags(
    ('FLY', 'Can fly'),
    ('FIREBALL', 'Can shoot fireballs'),
    ('INVISIBLE', 'Can turn invisible'),
)

Question

How can I add an __iter__ method to Abilities so that I can iterate over choices?

Why?

This way I can use things like

hero.abilities = Abilities.FLY | Abilities.FIREBALL

if hero.abilities & Abilities.FIREBALL:

for k, v in Abilities:
    print k, v

in my code without having to use any magic numbers or strings, and I can also save the set of flags to the DB as a single int, or display the list in a readable format.

Other improvements are welcome.

  • 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-15T15:41:19+00:00Added an answer on May 15, 2026 at 3:41 pm

    There’s no need to use a dynamic type here; I’d restructure this as a simple class, for example:

    class flags(object):
        def __init__(self, *opts):
            keys = [t[0] for t in opts]
            words = [t[1] for t in opts]
            nums = [2**i for i in range(len(opts))]
            self.attrs = dict(zip(keys,nums))
            self.choices = zip(nums,words)
    
        def __getattr__(self, a):
            return self.attrs[a]
    
        def __iter__(self):
            return iter(self.choices)
    
    Abilities = flags(
        ('FLY', 'Can fly'),
        ('FIREBALL', 'Can shoot fireballs'),
        ('INVISIBLE', 'Can turn invisible'),
    )
    
    print Abilities.FLY
    for k, v in Abilities:
        print k, v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the .def file, .lib file, the .dll, the source files. It's using
Source from copy import deepcopy class Field(object): def __init__(self): self.errors = [] class BaseForm(object):
I have 2 methods in a source file: def Foo puts hello puts bar
I have created a function for decoding url. from urllib import unquote def unquote_u(source):
def getSize(f): print StringIO(f) im = Image.open(StringIO(f)) size = im.size[0], im.size[1] return size def
I was browsing the Django source code and I saw this function: def colorize(text='',
I have code: def make_all_thumbs(source) sizes = ['1000','1100','1200','800','600'] threads = [] sizes.each do |s|
This is my source code def update @recipe = Recipe.find(params[:id]) respond_to do |format| if
Simple Groovy\Grails code: def start = { source.save() def result = getJson(hotelSite, eng +
Question source: SPOJ.. ORDERS def swap(ary,idx1,idx2): tmp = ary[idx1] ary[idx1] = ary[idx2] ary[idx2] =

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.