473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

RV: How to make an immutable instance

Thank you for your assistance.

I'll go in Decimal with a solution with property() and *single* underscore:

class C(object):

__slots__ = ('_x',)

def __init__(self, value):
self._x = value

def getx(self):
return self._x

x = property(getx)
This implies good security against accidental changes and saves me from a
*lot* (a lot!) of code changing in Decimal.py

Thanks again.

Facundo Batista
Desarrollo de Red
fb******@unifon.com.ar
(54 11) 5130-4643
Cel: 15 5097 5024


-----Mensaje original-----
De: Batista, Facundo
Enviado el: Jueves 17 de Junio de 2004 2:52 PM
Para: Python-List (E-mail)
Asunto: How to make an immutable instance
I'm working on Decimal, and one of the PEP requests is Decimal to be
immutable.
The closer I got to that is (in short):
class C(object):
__slots__ = ('__x',)
def __init__(self, value):
self.__x = value
def getx(self):
return self.__x
x = property(getx)
This way, you can not modify the instance:
import imm
c = C(4)
c.x 4 c.x = 3 Traceback (most recent call last):
File "<pyshell#4>", line 1, in -toplevel-
c.x = 3
AttributeError: can't set attribute c.a = 3 Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
c.a = 3
AttributeError: 'C' object has no attribute 'a' hash(c)

10777424
The problem is that you actually could, if you take the effort, to rebind
the __x name.
So, if you use this "immutable" class in a dict, and then you (on purpose)
modify it, you'll have different hashes.
Said that, how safer is this approach? Is there a better way?
Thank you all!
.. Facundo

.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . .
ADVERTENCIA
La información contenida en este mensaje y cualquier archivo anexo al mismo,
son para uso exclusivo del destinatario y pueden contener información
confidencial o propietaria, cuya divulgación es sancionada por la ley.
Si Ud. No es uno de los destinatarios consignados o la persona responsable
de hacer llegar este mensaje a los destinatarios consignados, no está
autorizado a divulgar, copiar, distribuir o retener información (o parte de
ella) contenida en este mensaje. Por favor notifíquenos respondiendo al
remitente, borre el mensaje original y borre las copias (impresas o grabadas
en cualquier medio magnético) que pueda haber realizado del mismo.
Todas las opiniones contenidas en este mail son propias del autor del
mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones
Personales S.A. o alguna empresa asociada.
Los mensajes electrónicos pueden ser alterados, motivo por el cual
Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación
cualquiera sea el resultante de este mensaje.
Muchas Gracias.

Jul 18 '05 #1
0 1084

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zalek Bloom | last post by:
I am learning about difference between String and StringBuffer classes. In a book it sayes that a String class is immutable, that means that one an instance of String class is created, the string...
6
by: Mapisto | last post by:
Hi, I've noticed that if I initialize list of integers in the next manner: >>> my_list = * 30 It works just fine, even if I'll try to assign one element: >>> id( my_list ) 10900116
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
16
by: fallenidol | last post by:
how do you create an immutable object in c#? thanks in advance
4
by: bbawa1 | last post by:
What does the term immutable mean? Could you please explain me with examples.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.