I didn't know about weak references. They're never mentioned in any books or
tutorials I've looked at. I guess I'll look into them.

On Mon, Jul 12, 2010 at 8:57 PM, Brian Fisher <[email protected]>wrote:

> You still haven't explained why referencing things by id is better for your
> needs than actually just referencing the object. Python's weakref module is
> perfect for this kind of thing, you can make a reference to an object that
> is fine for self-references (won't create cycles) and even cooler, the
> reference will become invalid when the object is deleted, so you'll never
> have the problem where the id somebody has stored has become invalid (or
> even worse, now points to a new object) because the original object was
> deleted and the guy with the id reference didn't know.
>
> so what I'm saying is like instead of doing stuff like this:
>     self.other_bullet_id = id(other_bullet)
> and this:
>     other_bullet = global_dictionary[self.other_bullet_id]
>     other_bullet.do_stuff()
>
> you'd do stuff like this:
>     self.other_bullet_ref = weakref.ref(other_bullet)
> and this:
>     other_bullet = self.other_bullet_ref()
>     other_bullet.do_stuff()
>
> and the cool part about that, is that "other_bullet" will be None, if the
> bullet object was deleted, when using weakrefs, so you can catch and handle
> that case just fine.
>
> While with id's, the id may have been reused and now refer to the wrong
> object, or will just simply be an invalid id when looking it up in the
> dictionary. Also, you'll never be sure if you were completely diligent in
> maintaining that dictionary of id's - so you won't know if the id is now
> invalid cause the bullet was deleted, or if you had a bug in the code that
> maintains the dict.
>
> ----
>
> basically, by trying to reference things by id, you've made a system that
> operates like using C/C++ pointers, with most (but not all) of the problems
> of such a system. Meanwhile, python gives you a ref-counted garbage
> collected system with weak references that solves all those problems, and is
> most likely much more efficient than your pointer re-implementation.
>
> ----
>
> so my question is the same - why are you doing this id system? What
> problems does it solve for you that weak references wouldn't?
>
>
>
> On Mon, Jul 12, 2010 at 4:09 PM, 音本四 <[email protected]> wrote:
>
>>
>>> (I ask because it seems like doing what you are doing would be a big ol'
>>> error prone hassle and waste of your time compared to other more commonly
>>> used techniques in python, but I can't figure out exactly what problem that
>>> dictionary approach was originally conceived of as a solution for)
>>>
>>
>> Simple: I needed instances to be able to reference themselves, and I
>> didn't feel like generating my own unique IDs and passing them on as a
>> parameter. :)
>> Why are you storing stuff by id() in a dictionary in the first place? When
>> would you want to look something up by it's id() when you wouldn't want just
>> to have a reference (possible a weakref) to the object itself?
>>
>>
>

Reply via email to