New to Python? Try this. #python #programming #code
Пікірлер: 45
@alvercury15 күн бұрын
You learned this TODAY? 🤔
@Heavy_Lvy15 күн бұрын
no he didnt😭
@granitium14 күн бұрын
Bruh
@ss_here_5014 күн бұрын
thats satire
@elevendarter11213 күн бұрын
Right after his first sentence I went to find out how old this video was :-)
@pallenda12 күн бұрын
😂 Yeah no way
@Michalski-oh7ii14 күн бұрын
I use this method almost since I started to learn programming.
@mattmurphy70303 күн бұрын
You run a programming channel and just learned this 😂😂😂
@davidgillies62012 күн бұрын
You should always try for RAII-like behaviour, if it's a paradigm that fits in your language's syntax.
@kamurashev15 күн бұрын
I think most (if not all) languages have thing like that, it’s a good thing, sure, I don’t understand though why it’s called “pythonic way” eg java has “try with resources” thing since 2011, and ages before that it had it as try/finally construction.
@ImNotActuallyChristian14 күн бұрын
Pythonic just means it’s the "proper" way to do something in python. It doesn’t mean it’s unique to the language
@erikkonstas14 күн бұрын
Fun fact: the formal meaning of "pythonic" can only be decreed by the PSF themselves!
@pharoah32712 күн бұрын
C# has the "using" keyword for this. It's nice. Although I would strongly suggest setting content variable to an empty string before the file IO so it isn't a NameError if it doesn't open the file properly.
@daviddanielng14 күн бұрын
Please devs, always use context manager if a class has it. I have known about it but always ignored it because my code works well, it bit me hard. I deployed to a droplet, monitored only to see the droplet was using 100% memory and 80%CPU after only 1 hour, my client was mad, i couldnt see which particular piece of code was causing the issue, i quickly scaled the drooplet while i investigate, the usage will go up to 95% after some time then i have to scale again. After investigating i saw that an expection was occuring resulting in tons of file never closing causing the computer resources usage to go up to 95%, that files are read and written to 10 times a second. Note: the file isn't the main one causing the issue, we also had playwright running which also was never closed.
@orcofnbu12 күн бұрын
this is the first time python actually made sense for me. such a usefull way to handle code block that dependent to outside information than runtime.
@VinceKully10 күн бұрын
If you just learned this then you have no business teaching python
@halopro7714 күн бұрын
I like the builtin pathlib more. Path("relative or absolute path").read_text()
@manhduong105012 күн бұрын
I always use it when I know about pathlibs, but some like using str rather than Path. I don't known why😢😢.
@pharoah32712 күн бұрын
I would strongly suggest setting content to an empty string in case something happens and the file IO fails. Or better, check if the file exists before opening it.
@VinceKully10 күн бұрын
Why would you need to preload the variable with an empty string? That doesn’t make any sense.
@pharoah32710 күн бұрын
@@VinceKully if it fails yet doesn't throw an exception, so program continues but content is never defined, you will get a NameError. Yet if you initialize it to an empty string, it will be defined regardless. Better practice that way. And in other languages this is even required to use it in an outer scope.
@Dan-The-Menace13 күн бұрын
Isn't that the standard way thats taught
@user-ji6ip7ou8d6 күн бұрын
C# has a using keyword that can be used this way like that: using (var sr = new StremReader()) { // do stuff } It's not something 'pythonic' whatever that means
@ErutTeru14 күн бұрын
with open("filename","r") as f: # "r" method open (read)
@pepsi31682 күн бұрын
you JUST learned that??
@DiomedesDominguez6 күн бұрын
Just like in Visual Basic 6
@markusklyver627710 күн бұрын
You finally learned about context managers?
@neuromorphing993312 күн бұрын
Who uses this old-school approach these days? A much more pythonic way to read/write files: from pathlib import Path print(Path(filepath).read_text(encoding="utf8")) # Bananas are berries.
@simplescreem55307 күн бұрын
Today??
@paulkingsmen42814 күн бұрын
why is it bad if the file never closes . i am a new dev
@mark1A10014 күн бұрын
it will remain in memory. it wont be cleaned properly. basically you lose that memory
@CoolModderJaydonX14 күн бұрын
It's akin to something like "using (var file = new FileStream)" in C#.
@TheCommunistRabbit14 күн бұрын
Because let's say your proggram opens 100 files, might be cuz we are coding a game or something.. we are opening one file to retrieve some data, and then never using it again. The problem is that the file is still in memory,, and the program uses more ram than it needs to. some bigger a.i projects use thousands of files so imagine how much ram would be wasted?
@pharoah32712 күн бұрын
Adobe Reader fails to close files. I have to close out the entire Reader app if a file was ever opened in a tab, even if it was closed awhile ago. So, yeah, it annoys the user to keep the file handle after the file has done its job and is no longer being actively used.
@Einstine1984Күн бұрын
NOBODY does that!
@locust7614 күн бұрын
Why is the file guaranteed to close though?
@Slackow14 күн бұрын
that's part of the with block, even if an error is thrown, the with block will call .close() on the file handle
@user-el6eh3er8c14 күн бұрын
@@Slackowso is it basically like the try with resources block in Java?