python: what is weakref? (intermediate - advanced) anthony explains

  Рет қаралды 9,282

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер: 30
@AlexGeorge
@AlexGeorge 4 ай бұрын
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
@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 👍🏼👍🏼👍🏼
@Khushpich
@Khushpich 3 жыл бұрын
Never delved in this before, this looks advanced but very interesting. Thanks for the content.
@sadhlife
@sadhlife 3 жыл бұрын
I always wondered why I'd ever want to create a weakref. Now I know, thanks!
@jamesfitzpatrick9607
@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.
@Jakub1989YTb
@Jakub1989YTb 4 ай бұрын
Would the WikiDictionary be suitable for "registering" instances in a class variable for faster lookup later?
@dddsa5951
@dddsa5951 3 жыл бұрын
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! : )
@anthonywritescode
@anthonywritescode 3 жыл бұрын
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
@dddsa5951
@dddsa5951 3 жыл бұрын
@@anthonywritescode thank you! That's very useful. Also the video is awesome : )
@rdean150
@rdean150 2 жыл бұрын
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.
@anthonywritescode
@anthonywritescode 2 жыл бұрын
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
@rdean150
@rdean150 2 жыл бұрын
@@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!
@anthonywritescode
@anthonywritescode 2 жыл бұрын
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
@gluteusminimus2134
@gluteusminimus2134 2 жыл бұрын
Great example. Keep it up!
@Quarky_
@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.
@anthonywritescode
@anthonywritescode 3 жыл бұрын
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_
@Quarky_ 3 жыл бұрын
@@anthonywritescode ah, thanks! I guess I need to try out a few implementations to let that sink in :)
@anthonywritescode
@anthonywritescode 3 жыл бұрын
tbf I've only used it once -- and that was a WeakKeyDictionary
@Quarky_
@Quarky_ 3 жыл бұрын
@@anthonywritescode I guess it's not a particularly common thing people need :)
@natieliyahu
@natieliyahu 2 жыл бұрын
Thanks. What do you think about finalizers?
@anthonywritescode
@anthonywritescode 2 жыл бұрын
avoid them like the plague -- if you need RAII-style finalization use context managers
@usmonbekravshanov6933
@usmonbekravshanov6933 2 жыл бұрын
Cool keyboard)
@anthonywritescode
@anthonywritescode 2 жыл бұрын
thanks! if you want to learn more about it I did a review here: kzbin.info/www/bejne/eovKdIiNm553kK8
@robert3258
@robert3258 Жыл бұрын
thanks! question: does python make use weakref internally anywhere? or is this just something an end developer might use if needed
@anthonywritescode
@anthonywritescode Жыл бұрын
can probably `git grep weakref` in `python/cpython`
@yarontaub
@yarontaub 3 жыл бұрын
Cool, thanks
@JohnZakaria
@JohnZakaria 3 жыл бұрын
This video made me subscribe :)
@riskzerobeatz
@riskzerobeatz 2 жыл бұрын
Nice :D
@mad_vegan
@mad_vegan 2 жыл бұрын
If you do (c := C()) then system.getrefcount(c) is 3. And system.getrefcount(C()) is 1.
@anthonywritescode
@anthonywritescode 2 жыл бұрын
the former is (1) the parameter (2) the variable c (3) the implicit `_` variable of the repl -- the latter is just the parameter
pip: requirements vs. constraints (intermediate) anthony explains #367
9:42
don't lru_cache methods! (intermediate) anthony explains #382
10:37
anthonywritescode
Рет қаралды 14 М.
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
python descriptors! (advanced) anthony explains #519
19:04
anthonywritescode
Рет қаралды 17 М.
Reuven Lerner: Avoiding memory leaks with "weakref"
24:20
PyCon Israel
Рет қаралды 3,3 М.
Is Functional Programming DEAD Already?
21:07
Continuous Delivery
Рет қаралды 69 М.
Python's most DISLIKED __dunder__ (and what to use instead)
9:59
what are python wheels? (intermediate - advanced) anthony explains #371
22:47
why rust libraries may never exist.
7:26
Low Level
Рет қаралды 277 М.
module level __getattr__ (intermediate) anthony explains #509
8:07
anthonywritescode
Рет қаралды 6 М.
how does python's module `__getattr__` actually work?
17:38
anthonywritescode
Рет қаралды 6 М.
python Generics (intermediate) anthony explains #430
13:43
anthonywritescode
Рет қаралды 16 М.
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН