RE: [Python-Dev] Decimal - context manipulation
[Paul Moore] #- One thing I couldn't find an obvious way of doing with Decimal is to #- obtain a copy of the current context, with a different (say) #- precision. The Context constructor defaults from #- DefaultContext rather #- than from the current context, and I can't see a way of modifying the #- precision of a Context object (is the "prec" attribute intended to be #- publicly writeable?) I assume that copy.copy works to copy a context, #- although maybe an explicit copy method would be useful?
from Decimal import * actual_context = getcontext() copied_context = copy.copy(actual_context) copied_context.prec = 5 +Decimal('123456789') Decimal( (0, (1, 2, 3, 4, 5, 6, 7, 8, 9), 0) ) actual_context.prec = 5 +Decimal('123456789') Decimal( (0, (1, 2, 3, 4, 6), 4L) )
Note the + in front of the creation of Decimal: that implies an operation and that's why the context get involved. Regarding a copy() method in Context class, don't know. It's really easy to implement, but don't know if the standard preference is to get a copy method inside each data type (I'm confused about dictionaries having a copy method and lists, for example, not having it). . 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.
[Batista, Facundo]
... Regarding a copy() method in Context class, don't know. It's really easy to implement, but don't know if the standard preference is to get a copy method inside each data type (I'm confused about dictionaries having a copy method and lists, for example, not having it).
There have always been several succinct ways to get a (shallow) copy of a list, like copied_list = list(original) and copied_list = original[:] For dicts, while you can do copied_dict = dict(original) today, at the time dict.copy() was introduced you could not do that. I usually add a .copy() method to my mutable types, like so: class MyClass: ... def __copy__(self): return build_a_shallow_copy_of(self) copy = __copy__ Note that __copy__ is a special name, and the standard copy.copy(thing) automatically invokes thing.__copy__() if thing has a __copy__() method. The "copy = __copy__" line then supplies that method directly to class users too under a conventional name. So, after the above, copy.copy(thing) and thing.copy() both work.
participants (2)
-
Batista, Facundo
-
Tim Peters