I Can't Believe THIS Works In Python #code #python #programming

preview_player
Показать описание
I can't believe this works in Python
Рекомендации по теме
Комментарии
Автор

Only do that for pure functions! Or else everything will catch fire

cover
Автор

Not only it can cause memory leaks, but one may accidentally introduce a bug that will be really hard to catch since cache is a decorator, not in a function itself

Smbrine
Автор

Only do this for pure functions - which are functions which ALWAYS return a specific output given a specific input.

kenarnarayaka
Автор

@cache
def randomNumber(n):

👁️ 👄 👁️

rutvikrana
Автор

Silence, python developers are learning about cache.

ffelegal
Автор

This only works for pure functions though

travelan
Автор

Don't use it on methods. Pure functions only. Bugs and leaks otherwise.

__mrmino__
Автор

Yes this is useful but also very basic. The python decorator is a nice QoL but in general if you care about performance you should do the caching manually using an array or dictionary. Also if you REALLY care about performance, then don't use python⭐️

bloom
Автор

Quite often, implementing your own cache can be much faster.

ttoommxxmmoott
Автор

What does "input_" do? I mean the underscore after input.

wedgeguyfr
Автор

Just don't use this if the function has side effects

MasonSchmidgall
Автор

what would happen if there's randomness in the func?

vorkutavorkutlag
Автор

Me and my friend were trying to optimize a program, and we turned it into a competition. He used your method. I used a dictionary. My method was significantly faster with only a few more lines than his.

diamond
Автор

Python scriptkiddie learns about memoization.

markusklyver
Автор

that's wicked. I'm putting that in tomorrow

Robert-qwlr
Автор

please guys just read std library first, all those tik-toks style videos is just docs

ordinarygg
Автор

if the process is already taking much of the memory, isnt it going to add up quickly with number of calls with different inputs

mpty
Автор

Hey! It's nice.
Can you please clarify what's difference between the following two:
1. @lru_cache
2. @cache

lru stands for Least Recently Used. They both decorators come from `functools` library, and work exactly same.

dipeshsamrawat
Автор

This can be good but the cache need to be clean or it's automatic and where he's going like ram or ssd ?

Kdr
Автор

Don't know why so many people on YouTube are so hyped about this. It's like you never heard of caching before, this approach is extremely limited and only works/benefits algorithms which return a constant result given an input, with these computations most of the time being instantaneous anyways (such as an equation) or precalculated. Not to even mention the memory usage

joopie
join shbcf.ru