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

  • Home
  • SEARCH
  • 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 344701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:00:54+00:00 2026-05-12T11:00:54+00:00

As python does not have concept of constants, would it be possible to raise

  • 0

As python does not have concept of constants, would it be possible to raise an exception if an ‘constant’ attribute is updated? How?

class MyClass():
    CLASS_CONSTANT = 'This is a constant'
    var = 'This is a not a constant, can be updated'

#this should raise an exception    
MyClass.CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'

#this should not raise an exception    
MyClass.var = 'updating this is fine'

#this also should raise an exception    
MyClass().CLASS_CONSTANT = 'No, this cannot be updated, will raise an exception'

#this should not raise an exception    
MyClass().var = 'updating this is fine'

Any attempt to change CLASS_CONSTANT as a class attribute or as an instance attribute should raise an exception.

Changing var as a class attribute or as an instance attribute should not raise an exception.

  • 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-12T11:00:54+00:00Added an answer on May 12, 2026 at 11:00 am

    Customizing __setattr__ in every class (e.g. as exemplified in my old recipe that @ainab’s answer is pointing to, and other answers), only works to stop assignment to INSTANCE attributes and not to CLASS attributes. So, none of the existing answers would actually satisfy your requirement as stated.

    If what you asked for IS actually exactly what you want, you could resort to some mix of custom metaclasses and descriptors, such as:

    class const(object):
      def __init__(self, val): self.val = val
      def __get__(self, *_): return self.val
      def __set__(self, *_): raise TypeError("Can't reset const!")
    
    class mcl(type):
      def __init__(cls, *a, **k):
        mkl = cls.__class__
        class spec(mkl): pass
        for n, v in vars(cls).items():
          if isinstance(v, const):
            setattr(spec, n, v)
        spec.__name__ = mkl.__name__
        cls.__class__ = spec
    
    class with_const:
      __metaclass__ = mcl
    
    class foo(with_const):
      CLASS_CONSTANT = const('this is a constant')
    
    print foo().CLASS_CONSTANT
    print foo.CLASS_CONSTANT
    foo.CLASS_CONSTANT = 'Oops!'
    print foo.CLASS_CONSTANT
    

    This is pretty advanced stuff, so you might prefer the simpler __setattr__ approach suggested in other answers, despite it NOT meeting your requirements as stated (i.e., you might reasonably choose to weaken your requirements in order to gain simplicity;-). But the techniques here might still be interesting: the custom descriptor type const is another way (IMHO far nicer than overriding __setattr__ in each and every class that needs some constants AND making all attributes constants rather than picking and choosing…) to block assignment to an instance attribute; the rest of the code is about a custom metaclass creating unique per-class sub-metaclasses of itself, in order to exploit said custom descriptor to the fullest and achieving the exact functionality you specifically asked for.

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

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As Jtolle indicated, this is simply not possible in VBA/VB6.… May 12, 2026 at 11:26 pm
  • Editorial Team
    Editorial Team added an answer The given function seems to do too much. Here's one… May 12, 2026 at 11:26 pm
  • Editorial Team
    Editorial Team added an answer Looks like your trying to replace the standard Ruby/Rubygems distribution… May 12, 2026 at 11:26 pm

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have an open source project containing both Python and C code. I'm wondering
I would like to set some vim options in one file in the comments
Does anyone know of a good software development framework or similar that has the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.