[pyar] Llevar un registro de las instancias de una clase

Luis I. Facciolo lifacciolo en gmail.com
Mar Feb 1 20:32:04 ART 2011


Y tambien quedaria mas prolijo si usaramos una lista en vez de un
diccionario.

>>> class Test:
...   reg = []
...   def __init__(self):
...       Test.reg.append(self)
...   def kill(self):
...       Test.reg.remove(self)
...
>>> a = Test()
>>> a.reg
[<__main__.Test instance at 0x02907940>]
>>> b = Test()
>>> a.reg
[<__main__.Test instance at 0x02907940>, <__main__.Test instance at
0x02907918>]
>>> b.reg
[<__main__.Test instance at 0x02907940>, <__main__.Test instance at
0x02907918>]
>>> b.kill()
>>> del b
>>> a.reg
[<__main__.Test instance at 0x02907940>]
>>> z = Test()
>>> z.reg
[<__main__.Test instance at 0x02907940>, <__main__.Test instance at
0x029078F0>]
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20110201/4bc70d68/attachment.html>


More information about the pyar mailing list