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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:18:32+00:00 2026-06-10T04:18:32+00:00

In python, is there a way to prevent adding new class variables after defining

  • 0

In python, is there a way to prevent adding new class variables after defining the object?

For example:

class foo:
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3

bar = foo()
try:
    bar.d = 4
except Exception, e:
    print "I want this to always print"

Alternatively, is there a way to count the number of variables in an object?

class foo:
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
    def count(self):
        ...

bar = foo()
if bar.count() == 3:
    print "I want this to always print"

The only way I thought of doing this was using a dictionary or list:

class foo:
    def __int__(self):
        self.dict = {'foo':1, 'bar':2} 
        self.len  = 2
    def chk():
        return self.len == len(self.list)

However, doing this feels rather cumbersome for python. (obj.dict[‘foo’]). I’d prefer just obj.foo if possible.

I want to have this so that I never accidentally declare a variable when I mean to change an existing one.

f = foo()
f.somename = 3
...
f.simename = 4 #this is a typo

if f.somename == 3:
    solve_everything()
  • 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-10T04:18:34+00:00Added an answer on June 10, 2026 at 4:18 am

    I suggest using __setattr__ to avoid the oddities of __slots__.

    You always have to be careful when messing with __setattr__, since it takes care of setting all instance attributes, including those you set in __init__. Therefore it has to have some way of knowing when to allow the setting of an attribute, and when to deny it. In this solution I’ve designated a special attribute that controls whether new attributes are allowed or not:

    class A(object):
        def __init__(self):
            self.a = 1
            self.b = 2
            self.c = 3
            self.freeze = True
    
        def __setattr__(self, attr, value):
            if getattr(self, "freeze", False) and not hasattr(self, attr):
                raise AttributeError("You shall not set attributes!")
            super(A, self).__setattr__(attr, value)
    

    Testing:

    a = A()
    try:
        a.d = 89
    except AttributeError:
        print "It works!"
    else:
        print "It doesn't work."
    a.c = 42
    print a.a
    print a.c
    a.freeze = False
    a.d = 28
    a.freeze = True
    print a.d
    

    Result:

    It works!
    1
    42
    28
    

    Also see gnibblers answer that wraps this concept neatly up in a class decorator, so it doesn’t clutter up the class definition and can be reused in several classes without duplicating code.


    EDIT:

    Coming back to this answer a year later, I realize a context manager might solve this problem even better. Here’s a modified version of gnibbler’s class decorator:

    from contextlib import contextmanager
    
    @contextmanager
    def declare_attributes(self):
        self._allow_declarations = True
        try:
            yield
        finally:
            self._allow_declarations = False
    
    def restrict_attributes(cls):
        cls.declare_attributes = declare_attributes
        def _setattr(self, attr, value):
            disallow_declarations = not getattr(self, "_allow_declarations", False)
            if disallow_declarations and attr != "_allow_declarations":
                if not hasattr(self, attr):
                    raise AttributeError("You shall not set attributes!")
            super(cls, self).__setattr__(attr, value)
        cls.__setattr__ = _setattr
    
        return cls
    

    And here’s how to use it:

    @restrict_attributes
    class A(object):
        def __init__(self):
            with self.declare_attributes():
                self.a = 1
                self.b = 2
                self.c = 3
    

    So whenever you want to set new attributes, just use the with statement as above. It can also be done from outside the instance:

    a = A()
    try:
        a.d = 89
    except AttributeError:
        print "It works!"
    else:
        print "It doesn't work."
    a.c = 42
    print a.a
    print a.c
    with a.declare_attributes():
        a.d = 28
    print a.d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to find out where a Python object was instantiated the
When writing python modules, is there a way to prevent it being imported twice
Is there a way in Python to list all the currently in-use drive letters
Is there a way to put together Python files, akin to JAR in Java?
Is there any way to run a Python script in Windows XP without a
Is there a Python way without using a subprocess to clone a git repository?
Is there a way to abort a python write operation in such a way
Is there a way in python to turn a try/except into a single line?
Is there a way in Python to print only the whole number portion of
Is there a way to upgrade the version of Python used in a virtual

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.