Properties always apply to instances, not classes.
A way to do this would be to define a metaclass that defines the property on its own instance method, since the class is an instance of its metaclass:
class AMeta(type): def __init__(self,name,bases,dict): self._instances = [] @property def instances(self): for inst_ref in self._instances: inst = inst_ref() if inst is not None: yield inst class A(object): __metaclass__ = AMeta def __init__(self): self._instances.append(weakref.ref(self))
Now this works as expected:
>>> foo=A() >>> bar = A() >>> for inst in A.instances: ... print inst <__main__.A object at 0x1065d7290> <__main__.A object at 0x1065d7990>
Daniel Roseman
source share