New To Python? Try THIS

  Рет қаралды 31,216

Indently

Indently

16 күн бұрын

New to Python? Try this. #python #programming #code

Пікірлер: 45
@alvercury
@alvercury 15 күн бұрын
You learned this TODAY? 🤔
@Heavy_Lvy
@Heavy_Lvy 15 күн бұрын
no he didnt😭
@granitium
@granitium 14 күн бұрын
Bruh
@ss_here_50
@ss_here_50 14 күн бұрын
thats satire
@elevendarter112
@elevendarter112 13 күн бұрын
Right after his first sentence I went to find out how old this video was :-)
@pallenda
@pallenda 12 күн бұрын
😂 Yeah no way
@Michalski-oh7ii
@Michalski-oh7ii 14 күн бұрын
I use this method almost since I started to learn programming.
@mattmurphy7030
@mattmurphy7030 3 күн бұрын
You run a programming channel and just learned this 😂😂😂
@davidgillies620
@davidgillies620 12 күн бұрын
You should always try for RAII-like behaviour, if it's a paradigm that fits in your language's syntax.
@kamurashev
@kamurashev 15 күн бұрын
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.
@ImNotActuallyChristian
@ImNotActuallyChristian 14 күн бұрын
Pythonic just means it’s the "proper" way to do something in python. It doesn’t mean it’s unique to the language
@erikkonstas
@erikkonstas 14 күн бұрын
Fun fact: the formal meaning of "pythonic" can only be decreed by the PSF themselves!
@pharoah327
@pharoah327 12 күн бұрын
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.
@daviddanielng
@daviddanielng 14 күн бұрын
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.
@orcofnbu
@orcofnbu 12 күн бұрын
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.
@VinceKully
@VinceKully 10 күн бұрын
If you just learned this then you have no business teaching python
@halopro77
@halopro77 14 күн бұрын
I like the builtin pathlib more. Path("relative or absolute path").read_text()
@manhduong1050
@manhduong1050 12 күн бұрын
I always use it when I know about pathlibs, but some like using str rather than Path. I don't known why😢😢.
@pharoah327
@pharoah327 12 күн бұрын
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.
@VinceKully
@VinceKully 10 күн бұрын
Why would you need to preload the variable with an empty string? That doesn’t make any sense.
@pharoah327
@pharoah327 10 күн бұрын
@@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-Menace
@Dan-The-Menace 13 күн бұрын
Isn't that the standard way thats taught
@user-ji6ip7ou8d
@user-ji6ip7ou8d 6 күн бұрын
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
@ErutTeru
@ErutTeru 14 күн бұрын
with open("filename","r") as f: # "r" method open (read)
@pepsi3168
@pepsi3168 2 күн бұрын
you JUST learned that??
@DiomedesDominguez
@DiomedesDominguez 6 күн бұрын
Just like in Visual Basic 6
@markusklyver6277
@markusklyver6277 10 күн бұрын
You finally learned about context managers?
@neuromorphing9933
@neuromorphing9933 12 күн бұрын
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.
@simplescreem5530
@simplescreem5530 7 күн бұрын
Today??
@paulkingsmen428
@paulkingsmen428 14 күн бұрын
why is it bad if the file never closes . i am a new dev
@mark1A100
@mark1A100 14 күн бұрын
it will remain in memory. it wont be cleaned properly. basically you lose that memory
@CoolModderJaydonX
@CoolModderJaydonX 14 күн бұрын
It's akin to something like "using (var file = new FileStream)" in C#.
@TheCommunistRabbit
@TheCommunistRabbit 14 күн бұрын
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?
@pharoah327
@pharoah327 12 күн бұрын
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
@Einstine1984 Күн бұрын
NOBODY does that!
@locust76
@locust76 14 күн бұрын
Why is the file guaranteed to close though?
@Slackow
@Slackow 14 күн бұрын
that's part of the with block, even if an error is thrown, the with block will call .close() on the file handle
@user-el6eh3er8c
@user-el6eh3er8c 14 күн бұрын
@@Slackowso is it basically like the try with resources block in Java?
@VinceKully
@VinceKully 10 күн бұрын
That’s how context managers work.
@artsbrand
@artsbrand 11 күн бұрын
Thanks a lot
5 Good Python Habits
17:35
Indently
Рет қаралды 412 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 95 М.
Получилось у Вики?😂 #хабибка
00:14
ХАБИБ
Рет қаралды 7 МЛН
Vivaan  Tanya once again pranked Papa 🤣😇🤣
00:10
seema lamba
Рет қаралды 31 МЛН
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 371 М.
Python Decorators in 15 Minutes
15:14
Kite
Рет қаралды 429 М.
Jamie reaches Winterfell & meets the Queen Daenerys|game of thrones season 8
4:48
Explained With Memes
Рет қаралды 1,1 МЛН
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 296 М.
Python Tutorial: if __name__ == '__main__'
8:43
Corey Schafer
Рет қаралды 2 МЛН
How I would learn to code (If I could start over)
9:16
Jason Goodison
Рет қаралды 4,6 МЛН
5 Uncommon Python Features I Love
15:09
Indently
Рет қаралды 136 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 384 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 272 М.