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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:18:37+00:00 2026-05-16T23:18:37+00:00

I want to control global variables (or globally scoped variables) in a way that

  • 0

I want to control global variables (or globally scoped variables) in a way that they are set only once in program initialization code, and lock them after that.

I use UPPER_CASE_VARIABLES for global variables, but I want to have a sure way not to change the variable anyway.

  • Does python provide that (or similar) feature?
  • How do you control the globally scoped variables?
  • 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-16T23:18:38+00:00Added an answer on May 16, 2026 at 11:18 pm

    ActiveState has a recipe titled Cᴏɴsᴛᴀɴᴛs ɪɴ Pʏᴛʜᴏɴ by the venerable Alex Martelli for creating a const module with attributes which cannot be rebound after creation. That sounds like what you’re looking for except for the upcasing — but that could be added by making it check to see whether the attribute name was all uppercase or not.

    Of course, this can be circumvented by the determined, but that’s the way Python is — and is considered to be a "good thing" by most folks. However, to make it a little more difficult, I suggest you don’t bother adding the supposedly obvious __delattr__ method since people could then just delete names and then add them back rebound to different values.

    This is what I’m taking about:

    Put in const.py:

    # from http://code.activestate.com/recipes/65207-constants-in-python
    class _const:
        class ConstError(TypeError): pass  # Base exception class.
        class ConstCaseError(ConstError): pass
    
        def __setattr__(self, name, value):
            if name in self.__dict__:
                raise self.ConstError("Can't change const.%s" % name)
            if not name.isupper():
                raise self.ConstCaseError('const name %r is not all uppercase' % name)
            self.__dict__[name] = value
    
    # Replace module entry in sys.modules[__name__] with instance of _const
    # (and create additional reference to it to prevent its deletion -- see
    #  https://stackoverflow.com/questions/5365562/why-is-the-value-of-name-changing-after-assignment-to-sys-modules-name)
    import sys
    _ref, sys.modules[__name__] = sys.modules[__name__], _const()
    
    if __name__ == '__main__':
        import __main__  as const  # Test this module...
    
        try:
            const.Answer = 42  # Not OK to create mixed-case attribute name.
        except const.ConstCaseError as exc:
            print(exc)
        else:  # Test failed - no ConstCaseError exception generated.
            raise RuntimeError("Mixed-case const names should't be allowed!")
    
        try:
            const.ANSWER = 42  # Should be OK, all uppercase.
        except Exception as exc:
            raise RuntimeError("Defining a valid const attribute should be allowed!")
        else:  # Test succeeded - no exception generated.
            print('const.ANSWER set to %d raised no exception' % const.ANSWER)
    
        try:
            const.ANSWER = 17  # Not OK, attempt to change defined constant.
        except const.ConstError as exc:
            print(exc)
        else:  # Test failed - no ConstError exception generated.
            raise RuntimeError("Shouldn't be able to change const attribute!")
    

    Output:

    const name 'Answer' is not all uppercase
    const.ANSWER set to 42 raised no exception
    Can't change const.ANSWER
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use variables to set my timer? I want to control delay
I only want my JavaScript to run once, but I cannot control how many
lets say that I have a global style set for ItemsContainer control, and this
I want to control a streaming music website with global hotkeys, so I can
I want to make some re-useable, somewhat-dynamic TSQL code that can be called within
I am looking to set up a central point of control for settings that
We have a really old legacy code base that uses globals like they're going
Greetings, I have a control and list of variables and I want in the
I want to control the mouse pointer with my application and be able to
I want to control does an element exist in document with its ID, when

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.