Immutable object: Difference between revisions

Content deleted Content added
m Python: Update Python example code from deprecated Python 2 to Python 3
Line 292:
 
=== Python ===
In [[Python (programming language)|Python]], some built-in types (numbers, booleans, strings, tuples, frozensets) are immutable, but custom classes are generally mutable. To simulate immutability in a class, one shouldcould override attribute setting and deletion to raise exceptions:
 
<syntaxhighlight lang="python">
class ImmutablePoint(object):
"""An immutable class with two attributes 'x' and 'y'."""
 
Line 308:
# We can no longer use self.value = value to store the instance data
# so we must explicitly call the superclass
super(ImmutablePoint, self).__setattr__('x', x)
super(ImmutablePoint, self).__setattr__('y', y)
</syntaxhighlight>