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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:27:17+00:00 2026-06-01T16:27:17+00:00

I have a class with static variables for error/status code lookup. Take HTTP status

  • 0

I have a class with static variables for error/status code lookup.
Take HTTP status code as example

class Foo(object):
    OK = 200
    Not_Modified = 304
    Forbidden = 403
    Internal_Server_Error = 500

Now I need to retrieve the verbal status (‘OK’, ‘Not_Modified’, etc) base on the code (200, 403, etc). I can’t modify the structure of the class since other programs are using it. So I created a dictionary description_by_val that contain the {code : description}:

from collections import Hashable

class Foo(object):
    OK = 200
    Not_Modified = 304
    Forbidden = 403
    Internal_Server_Error = 500
    description_by_val = dict((value, key)
        for key, value in locals().iteritems()
            if not key.startswith("__") and value and isinstance(value, Hashable))


>>> Foo.description_by_val[200]
'OK'

Now I have questions in terms of performance and code practice.

  • Every time I call Foo.description_by_val will it cause the dictionary be regenerated? this is not good even the data set is very small, because this will get call millions times.
  • It that approach simply bad practice? I don’t want to manually create the dictionary by hand and I think it should be a static variable.

any thought?

update:

My colleague just pointed out to me that I could print something during the creation of description_by_val to find out if it will be regenerated.

>>> from collections import Hashable
>>> 
>>> def show(key):
...     print key
...     return True
... 
>>> 
>>> class Foo(object):
...     OK = 200
...     Not_Modified = 304
...     Forbidden = 403
...     Internal_Server_Error = 500
...     description_by_val = dict((value, key)
...         for key, value in locals().iteritems()
...             if not key.startswith("__") and key and isinstance(value, Hashable) and show(key))
... 
OK
Forbidden
Internal_Server_Error
Not_Modified
>>> 
>>> Foo.description_by_val
{200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'}
>>> Foo.description_by_val
{200: 'OK', 304: 'Not_Modified', 403: 'Forbidden', 500: 'Internal_Server_Error'}
>>> Foo.description_by_val[200]
'OK'

I’m now happy with that I don’t have to worry about the performance. I want to find out why it behave like 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-06-01T16:27:18+00:00Added an answer on June 1, 2026 at 4:27 pm

    Your idea is sound. The dictionary is not regenerated every time, only the first time it is created. Lookup is efficient and reliable, and this is unlikely to cause problems that I can see. Using this kind of reverse dictionary is pretty common, and you have the isinstance(value, Hashable) check in a good place as well. You should be fine.

    — Edited —

    Your code is fine, I just missed the trailing paren.

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

Sidebar

Related Questions

Is it possible or makes sense to have static dynamic variables on a class,
I have been sharing database variables using the following code: Namespace DataAccessVariables Public Class
I would like to have class static variables as states, but Objective C disallowed
I have read many documentation about static variables. Here is one example which I
I have a class with static method which has a local static variable. I
I have a class with a static method modifying a static variable as follows,
WorkAround: I have declared a class level Public static variable and initialized with a
If you have a template class with a static variable, is there any way
I have a base class A with a constant static variable a. I need
I have a static variable partner in the class. And I want to set

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.