diff --git a/_drafts/python-object-replacement.md b/_drafts/python-object-replacement.md index 4f91858..835fe2c 100644 --- a/_drafts/python-object-replacement.md +++ b/_drafts/python-object-replacement.md @@ -14,7 +14,10 @@ object has made its way throughout your code. It lives inside lists, class attributes, maybe even inside some closures. You want to completely replace this object with another one; that is to say, you want to find all references to object `A` and replace them with object `B`, enabling `A` to be garbage -collected. +collected. This has some interesting implications for special object types. If +you have methods that are bound to `A`, you want to rebind them to `B`. If `A` +is a class, you want all instances of `A` to become instances of `B`. And so +on. _But why on Earth would you want to do that?_ you ask. I'll focus on a concrete use case in a future post, but for now, I imagine this could be useful in some @@ -38,7 +41,7 @@ a = [1, 2, 3, 4] We are creating a list object with four integers, and binding it to the name `a`: -
[1, 2, 3, 4]
[Not supported by viewer]
a
[Not supported by viewer]
+%3L[1, 2, 3, 4]aaa->L In each of the following examples, we are creating new _references_ to the list object, but we are never duplicating it. Each reference points to the same @@ -128,15 +131,16 @@ function closures Certainly, not every case is handled above, but it seems to cover the vast majority of instances that I've found through testing. There are a number of reference relations in Guppy that I couldn't figure out how to replicate -without doing something insane (`R_CELL` and `R_STACK`), so some obscure -replacements are likely unimplemented. +without doing something insane (`R_HASATTR`, `R_CELL`, and `R_STACK`), so some +obscure replacements are likely unimplemented. Some other kinds of replacements are known, but impossible. For example, replacing a class object that uses `__slots__` with another class will not work if the replacement class has a different slot layout and instances of the old -class exist. Furthermore, it doesn't work for references stored in the code of -C extensions, since there's effectively no way for us to track these, but this -is an exceptional circumstance. +class exist. More generally, replacing a class with a non-class object won't +work if instances of the class exist. Furthermore, references stored in data +structures managed by C extensions cannot be changed, since there's no good way +for us to track these. Remaining areas to explore include behavior when metaclasses and more complex descriptors are involved. Implementing a more complete version of `replace()`