[wingide-users] Enumerating instances of a class

Tom Stambaugh tms at zeetix.com
Sun Mar 21 17:31:04 MDT 2010


I get it, thanks Michael.

Thanks,
Tom

Michael Foord wrote:
> What I'm suggesting is something along the lines of:
> 
>     OldClass.__dict__.clear()
>     OldClass.__dict__.update(NewClass.__dict__)
> 
> That would make all the instances of the new class *effectively* 
> instances of the new class without cloning and without having to 
> traverse all objects.
> 
> Michael
> 
> On 21/03/2010 23:19, Tom Stambaugh wrote:
>> Yeah, I've got that under control.
>>
>> I'm snarfing the Envy/Smalltalk behavior that loads a new edition of a 
>> class from a datastore. The new (Python) class isn't visible to the 
>> environment yet, and won't be until I finish this operation.
>>
>> The idea is to atomically collect all the instances of the existing 
>> class, clone them as instances of the new class, ditch the old class, 
>> then install the new class. I do all this in carefully managed context 
>> that (hopefully) ensures that nothing leaks into the outside 
>> environment until I'm done. While it would be a happy accident if 
>> Python does the right thing automatically, my framework does all this 
>> with other languages that don't (necessarily).
>>
>> Thanks for your help, I'm always a little reluctant to go poking 
>> around inside gc.
>>
>> Thx,
>> Tom
>>
>> Michael Foord wrote:
>>> On 21/03/2010 23:06, Tom Stambaugh wrote:
>>>> Ah, I'm on the right track then. I'm just now playing with 
>>>> gc.get_referrers.
>>>>
>>>> I've thought about tracking instances myself as you suggest, but 
>>>> I've avoided it because the allInstances method is called only very 
>>>> occasionally; I'd rather restrict the overhead to when I need it 
>>>> rather than all the time.
>>>>
>>>> In this case, I'm about to change a class that has instances, and I 
>>>> need to mutate the instances of the existing class so that they 
>>>> (after suitable massaging) end up being instances of the new class.
>>>
>>> Well - if you modify the original class instead then they are 
>>> *already* instances of that class. (Classes are mutable and changes 
>>> to a class are visible on instances - unless you need to add / mutate 
>>> instance members.)
>>>
>>> Michael
>>>
>>>>
>>>> Thx,
>>>> Tom
>>>>
>>>> Michael Foord wrote:
>>>>> On 21/03/2010 22:45, Tom Stambaugh wrote:
>>>>>> Anybody know how to enumerate the instances of a Python class?
>>>>>>
>>>>>> The Smalltalk equivalent, on any Smalltalk "Behavior" instance 
>>>>>> (any class or Metaclass), is "allInstances". It is implemented as 
>>>>>> a primitive; the underlying VM traverses the active object space 
>>>>>> collecting objects whose class pointer matches the class in question.
>>>>>>
>>>>>
>>>>> There is nothing builtin in Python directly equivalent, but you can 
>>>>> add it yourself using something like gc.get_objects():
>>>>>
>>>>> http://docs.python.org/library/gc.html
>>>>>
>>>>> If you want to keep track of instances a much better way of doing 
>>>>> it is have your class(es) keep of weak references to all instances 
>>>>> it creates.
>>>>>
>>>>> All the best,
>>>>>
>>>>> Michael
>>>>>
>>>>>> I'm hoping that there's some kind of builtin method that does the 
>>>>>> same in Python. I *really* don't want to have to extend Python 
>>>>>> myself.
>>>>>>
>>>>>> Thx,
>>>>>> Tom
>>>>>
>>>>>
>>>>
>>>
>>>
>>
> 
> 

-- 
Tom Stambaugh, Founder
Zeetix LLC/ZeeGuide/ZeeForge
27 Auburn Street
Brookline, MA 02446
617-734-8934 (land)
617-721-0446 (cell)


More information about the wingide-users mailing list