The CPython implementation detail about object address reuse mentioned in *explains **#343* makes a prominent appearance in this episode! I can't help but notice it since obtaining that bit of knowledge.
@PanduPoluan Жыл бұрын
Thanks for the video! Really helpful in making me understand the weakref module. And I like the "bloopery" part there 😄 Very good educative video 👍🏼👍🏼👍🏼
@Khushpich3 жыл бұрын
Never delved in this before, this looks advanced but very interesting. Thanks for the content.
@sadhlife3 жыл бұрын
I always wondered why I'd ever want to create a weakref. Now I know, thanks!
@jamesfitzpatrick9607 Жыл бұрын
Is it wrong that the {self=} in the f string amazed me. It's going to be so much faster doing my logs from now on.
@Jakub1989YTb4 ай бұрын
Would the WikiDictionary be suitable for "registering" instances in a class variable for faster lookup later?
@dddsa59513 жыл бұрын
Antony, how come you typed `fg` and got your python restored with all the imports from the previous session already there? How does this magic work, I need it! : )
@anthonywritescode3 жыл бұрын
I accidentally bumped a key on my keyboard which triggers `^Z` which backgrounded the program. I then used `fg` to foreground it -- I should probably put that into a separate video. I tend to try and avoid foregrounding / backgrounding when streaming / making videos because it makes it a lot harder to follow what I'm doing
@dddsa59513 жыл бұрын
@@anthonywritescode thank you! That's very useful. Also the video is awesome : )
@rdean1502 жыл бұрын
Very useful and exactly what I was looking for to prevent a memory leak due to circular references between a data object and the "behavioral" object that acts as its API. Still can't shake the feeling that this may be an anti-pattern though, since I'm reaching for such a rarely-used tool in order to implement it.
@anthonywritescode2 жыл бұрын
are you sure that's the cause of your leak? python's garbage collector is usually able to collect cycles without needing weakref -- weakref is usually only helpful to get them collected in an earlier collection generation
@rdean1502 жыл бұрын
@@anthonywritescode To be fair, I may be guilty of premature optimization here. This package is still in its early stages and I have not actually seen any memory leak is occurring, just trying to ensure no such leak will occur. Having a child object hold a reference to its parent raised some alarm bells in my head several days ago and I happened upon this video today that seemed like a good way to avoid a potential leak. But if the reference counter is already smart enough to prevent this then it was much ado about nothing. Which is even better news!
@anthonywritescode2 жыл бұрын
hopefully! it might make it ~slightly faster to garbage collect but unless you've got a lot of your cycle objects it's probably not going to matter
@gluteusminimus21342 жыл бұрын
Great example. Keep it up!
@Quarky_3 жыл бұрын
Thanks a lot! This has always confused me a bit. Q: When would I want to use a ref instead of proxy? Seems like the proxy is the easier one to use.
@anthonywritescode3 жыл бұрын
there's two key differences: - ref() gives you a temporary strong reference - proxies are never perfect (they leak implementation details, such as `type(...)` or other special methods) and so they can't always be substituted for the actual value
@Quarky_3 жыл бұрын
@@anthonywritescode ah, thanks! I guess I need to try out a few implementations to let that sink in :)
@anthonywritescode3 жыл бұрын
tbf I've only used it once -- and that was a WeakKeyDictionary
@Quarky_3 жыл бұрын
@@anthonywritescode I guess it's not a particularly common thing people need :)
@natieliyahu2 жыл бұрын
Thanks. What do you think about finalizers?
@anthonywritescode2 жыл бұрын
avoid them like the plague -- if you need RAII-style finalization use context managers
@usmonbekravshanov69332 жыл бұрын
Cool keyboard)
@anthonywritescode2 жыл бұрын
thanks! if you want to learn more about it I did a review here: kzbin.info/www/bejne/eovKdIiNm553kK8
@robert3258 Жыл бұрын
thanks! question: does python make use weakref internally anywhere? or is this just something an end developer might use if needed
@anthonywritescode Жыл бұрын
can probably `git grep weakref` in `python/cpython`
@yarontaub3 жыл бұрын
Cool, thanks
@JohnZakaria3 жыл бұрын
This video made me subscribe :)
@riskzerobeatz2 жыл бұрын
Nice :D
@mad_vegan2 жыл бұрын
If you do (c := C()) then system.getrefcount(c) is 3. And system.getrefcount(C()) is 1.
@anthonywritescode2 жыл бұрын
the former is (1) the parameter (2) the variable c (3) the implicit `_` variable of the repl -- the latter is just the parameter