Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
How to make a specific instance of a class immutable?

Having an instance c of a class C, I would like to make c immutable, but other instances of C dont have to.

Is there an easy way to achieve this in python?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can't make Python classes fully immutable. You can however imitate it:

class C:
    _immutable = False
    def __setattr__(self, name, value):
        if self._immutable:
            raise TypeError(f"Can't set attribute, {self!r} is immutable.")
        super().__setattr__(name, value)

Example:

>>> c = C()
>>> c.hello = 123
>>> c.hello
123
>>> c._immutable = True
>>> c.hello = 456
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in __setattr__
TypeError: Can't set attribute, <__main__.C object at 0x000002087C679D20> is immutable.

If you wish to set it at initialization, you can add an __init__ like so:

class C:
    _immutable = False
    def __init__(self, immutable=False):
        self._immutable = immutable
    def __setattr__(self, name, value):
        if self._immutable:
            raise TypeError(f"Can't set attribute, {self!r} is immutable.")
        super().__setattr__(name, value)

Keep in mind you can still bypass it by accessing and modifying the __dict__ of the instance directly:

>>> c = C(immutable=True)
>>> c.__dict__["hello"] = 123
>>> c.hello
123

You may attempt to block it like so:

class C:
    _immutable = False
    def __init__(self, immutable=False):
        self._immutable = immutable
    def __getattribute__(self, name):
        if name == "__dict__":
            raise TypeError("Can't access class dict.")
        return super().__getattribute__(name)
    def __setattr__(self, name, value):
        if self._immutable:
            raise TypeError(f"Can't set attribute, {self!r} is immutable.")
        super().__setattr__(name, value)

But even then it's possible to bypass:

>>> c = C(immutable=True)
>>> object.__getattribute__(c, "__dict__")["hello"] = 123
>>> c.hello
123
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!