[pyar] Sobre Mutable / Immutable

Claudio Freire klaussfreire en gmail.com
Mie Sep 1 19:37:50 ART 2010


2010/9/1 Martin Cerdeira <martincerdeira en gmail.com>

> 2010/9/1 Claudio Freire <klaussfreire en gmail.com>
>
>>
>> Por... referencia débil?
>>
>> def incrementar(x):
>>    x = x+1
>>
>
> Esto no incrementa el valor de la variable x dentro de la función y nada
> más (como si fuera por valor) ?
>

Eso depende de lo que sea x.

>>> class Integer:
...     def __init__(self, val):
...         self.value = val
...     def __add__(self, other):
...         self.value += other
...         return self
...     def __str__(self):
...         return str(self.value)
...     def __repr__(self):
...         return 'Integer(%d)' % (self.value,)
...
>>> def incrementar(x):
...     x = x + 1
...
>>> x = Integer(3)
>>> incrementar(x)
>>> x
Integer(4)
>>>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20100901/8496be41/attachment.html>


More information about the pyar mailing list