5 Wacky Python Features

  Рет қаралды 24,473

Indently

Indently

Күн бұрын

Пікірлер: 86
@two_number_nines
@two_number_nines 4 күн бұрын
IIFE is used to hide variables within a scope. it executes once, pops off the stack and wipes from memory. its used for naming collision safety.
@Indently
@Indently 4 күн бұрын
That's good to know!
@maleldil1
@maleldil1 4 күн бұрын
Or you could just write a function and call the function. I don't see how this is better. Maybe you don't want the function around after it's used? Doesn't sound like a good reason to use something so unintuitive.
@two_number_nines
@two_number_nines 4 күн бұрын
@@maleldil1 yes IIFE makes the function deliberately unusable again. it frees up the memory used for its object
@Indently
@Indently 3 күн бұрын
I personally didn’t know about the memory part in Python, I appreciate you sharing.
@two_number_nines
@two_number_nines 3 күн бұрын
@@Indently i made it up kinda. my assumption is python/js would be wise enough to wipe the bytecode once the object pops off the stack
@maleldil1
@maleldil1 4 күн бұрын
The warlus operator is also useful when you want to do some map+filter. For example, instead of [word.strip() for word in words if word.strip()], where you're calling `word.strip` twice, you can do [w for word in words if (w := word.strip())]. This is particularly useful if your mapping function is complex or expensive, so you only do it once.
@TAP7a
@TAP7a 3 күн бұрын
“List of list of string… oh I think I’m high” The programming experience in a single moment
@DeLittleCat
@DeLittleCat 4 күн бұрын
The walrus operator is also pretty useful for list comprehension.
@sahasananth987
@sahasananth987 3 күн бұрын
How
@chary9407
@chary9407 21 сағат бұрын
@@sahasananth987saves compute for expensive expressions that you only wanna do once to get truthy values like [ w for word in words.split() if words.split() ]
@amaarquadri
@amaarquadri 4 күн бұрын
IIFE is also used in C++ for declaring const local variables that need to be initialized in a multi-step way that would normally prevent them from being marked const.
@gurupartapkhalsa6565
@gurupartapkhalsa6565 3 күн бұрын
@Indently it's called the complement operator, it returns the bitwise complement of the number ... Signed Integers in modern computing follow the twos complement standard, which is why -1 is the same as max unsigned
@pfizerpflanze
@pfizerpflanze 4 күн бұрын
Bitwise operator is not magic, it depends on the binary representation of integers. On floats numbers I'm pretty sure it doesn't work that way at all!
@vytah
@vytah 4 күн бұрын
It doesn't, floats cause a TypeError
@ConsciousAtoms
@ConsciousAtoms Күн бұрын
To decrement x, you simply swap the operators: ~-x results in x - 1 (if x is an integer).
@islandcave8738
@islandcave8738 3 күн бұрын
The reason to do the last one is it prevents program start time from being called any other time than the program start time, so it will never return the wrong value. In statically typed languages you could use static variables to achieve this instead.
@RadenVijaya
@RadenVijaya 22 сағат бұрын
Walrus, reminds me to Turbo Pascal 😂
@jamesarthurkimbell
@jamesarthurkimbell 23 сағат бұрын
I used walrus quite a bit until I started using pattern matching, which often ends up taking care of the same process: get something, check it, name it, all in one.
@adryelbarros3250
@adryelbarros3250 4 күн бұрын
IIFE is a way of solving problems with naming colisions and public variables in JavaScript If you have an html file with two script tags, both will create variables in a global scope, so if your script declares an asdf variable and some third party script also uses this as a name, one will override the other Variables inside a function are restricted by the function's scope, so IIFE are a way to create new scopes and dont bleed variables to other scripts
@b_dawg_17
@b_dawg_17 3 күн бұрын
Does the LET keyword solve this problem?
@DovRine
@DovRine 3 күн бұрын
@@b_dawg_17 partially. the module pattern solves this problem
@trinhvanquan8443
@trinhvanquan8443 2 күн бұрын
@@b_dawg_17 let doesn’t solve all the problems. IIFE not only isolate variable but also function itself
@victorsago
@victorsago 2 күн бұрын
It made sense in old JS, which lacked block scoping. But what value is there in introducing this in Python?
@patfre
@patfre 4 күн бұрын
6:30 this really hurts making a float be called a decimal. For reference decimal in most programming languages refers to a 128 bit floating point number with very high precision so calling it that but make it a float is just so cursed. Yes I know that’s what its usually called on a day to day basis but it just feels so wrong when in programming its not
@Nick-the-fox
@Nick-the-fox Күн бұрын
I am seeing that way of declaring variables for the first time. I HAD NO IDEA THAT YOU COULD JUST DO var: str = “Hello”
@anywallsocket
@anywallsocket Күн бұрын
It’s just so python doesn’t have to guess your type, it’s not necessary
@patfre
@patfre 4 күн бұрын
6:16 This also exists in many other languages and is not really wacky I find this very useful. C# is what I generally use and it has it too
@yahyasalimi3254
@yahyasalimi3254 3 күн бұрын
Time to confuse my colleagues
@asagiai4965
@asagiai4965 4 күн бұрын
So the walrus operator here acts like in-line assignment operator I don't think they are really that wacky.
@vogonjelc
@vogonjelc 4 күн бұрын
The old pascal assignment operator.
@jamesross1003
@jamesross1003 4 күн бұрын
Yeah, sort of. Python most likely did take this as an inherited operator from pascal, but it was taken from unix command line for (with a very similar syntax if I recall properly). Don't take it as absolute as it has been many years since I worked any with a unix system. Kudos for the catch, sharp eye there.
@y2ksw1
@y2ksw1 2 күн бұрын
The matrix operator is particularly useful!
@negative-example
@negative-example Күн бұрын
what font do you use? looks very good!
@HubertNafalski
@HubertNafalski 4 күн бұрын
Hi, Bob!
@DrDeuteron
@DrDeuteron 4 күн бұрын
your matmul should return: type(self)(zip(*[[sum(map(operator.mul, col, row)) for col in self] for row in zip(*other)]))
@brunopanizzi
@brunopanizzi 4 күн бұрын
So pythonic and readable 😍😍
@DrDeuteron
@DrDeuteron 4 күн бұрын
@@brunopanizzi I know, the list comp is lame. I'd rather map to an itertools thing for ZERO loops, but I was in a hurry.
@DrDeuteron
@DrDeuteron 4 күн бұрын
@@brunopanizzi wait, no. It _is_ readable. It says: sum the element wise product of rows and columns for each column in right multiplcand and each row in the left one, and then construct a new instance of the class with the results. (the final zip is because I f'd up an computed (AB).T b/c it was in hurry).
@MeinDeutschkurs
@MeinDeutschkurs Күн бұрын
I cannot see the benefit of @ lambda, because I can write multiple lines of code outside a function. And if I want to indent it, I can write if true colon.
@E2EOnpremUser
@E2EOnpremUser 13 сағат бұрын
instead of @lambda you could also use @dataclass and put the variables into a class with frozen=true
@Indently
@Indently 13 сағат бұрын
It doesn’t call itself though, which is the magic of the cursed lambda decorator.
@E2EOnpremUser
@E2EOnpremUser 10 сағат бұрын
@@Indently also class variables are declared when a class is being constructed, only once
@dmitryutkin9864
@dmitryutkin9864 3 күн бұрын
It was interesting!
@terrawest9500
@terrawest9500 2 күн бұрын
I just learned that Python got typing like GDscript (I've been using python for tools for years)
@mrab4222
@mrab4222 3 күн бұрын
10:21 There's also the opposite that subtracts 1: ~-value
@Winter0192
@Winter0192 Күн бұрын
11:21
@dipeshsamrawat7957
@dipeshsamrawat7957 4 күн бұрын
Thank you 😊
@landsgevaer
@landsgevaer 4 күн бұрын
More importantly though, is it two eyes and two tusks, or two nostrils and two tusks? (8:=
@AbhishekVaid
@AbhishekVaid 21 сағат бұрын
I found matmul operator explanation not inline with theme of this video. It's just a operator overloading example, nothing to do with mat multiplication.
@75hilmar
@75hilmar 2 күн бұрын
Reminds me of Haskell
@fppasquier
@fppasquier 3 күн бұрын
Hi Bob!
@koj3sama
@koj3sama 4 күн бұрын
Walrus só much poweful 😮
@senge1337
@senge1337 3 күн бұрын
Sandra? Where is Luigi? 😢
@Indently
@Indently Күн бұрын
Can’t be too careful with Nintendo trademarks these days, Sandra has stepped in
@DrDeuteron
@DrDeuteron 4 күн бұрын
the walrus operator is ok, but it violates one of Guido's principles: --> Never mix assignments and expressions which is why we don't have ++n, n++ type stuff.
@MrShadowThief
@MrShadowThief 4 күн бұрын
Wacky principle. Walrus operator, ++n and n++ are great.
@sectorgamma
@sectorgamma 4 күн бұрын
usually that new assignment is only meant to be used again on the same line or the one after, so without having to define 10 million variables before that line which arguably hurts readability more and makes the code unnecessarily long. Unless you're really confusing yourself with the names of the new variables then I see nothing wrong with the walrus operator. I care less about principles which are sometimes a bit arbitrary, and more about practicality.
@DrDeuteron
@DrDeuteron 4 күн бұрын
@@MrShadowThief OK, you tell Guido his principles are wacky.
@DavideCanton
@DavideCanton 4 күн бұрын
​@@DrDeuteron They are opinions, so they can be discussed. I use the walrus a lot, it's handy to assign variables that make sense only inside the body of an if. Guido is not better than anyone else.
@vytah
@vytah 3 күн бұрын
you can call a function in an expression and that function can do assignments, so this principle has been violated since always
@alipkamate1551
@alipkamate1551 4 күн бұрын
@ symbols that's what I use in all my machine learning code to matrix multiplication nice to see here 😊
@Davo8899
@Davo8899 3 күн бұрын
Only 5?
@jadencorr6897
@jadencorr6897 3 күн бұрын
Please, never use lambda annotation for the sake of gods
@spacelem
@spacelem 4 күн бұрын
They're not all that wacky, Julia can do pretty much all of that too. It doesn't need a walrus operator, = will do, because as a Lisp derivative, statements are expressions and return a value.
@gdclemo
@gdclemo Күн бұрын
The point of the walrus operator is that it's easy to mix up == and =. = in a statement is unambiguous because you probably wouldn't want to do a comparison and throw away the result, but = in an expression is likely to be a mistyped ==; this can happen a lot in C and C++ if your IDE or compiler don't warn you about it.
@spacelem
@spacelem Күн бұрын
@@gdclemo I get that mistakes can be make, but overall I find statements being expressions to be very handy (and overall it's a shame that Python doesn't do that). Plus if you want to be careful and not overwrite a variable, you could always use local: if (local n = sum(x)) > 5 println("$n > 5") else println("$n ≤ 5") end then that n won't overwrite an existing n, and == would be an error. Plus you could make a macro to turn := into the above (I'm not that great at macros, but it shouldn't be too difficult).
@spacelem
@spacelem Күн бұрын
@@gdclemo sorry for the double post there, I thought it had deleted the first attempt, but then it came back.
@gdclemo
@gdclemo Күн бұрын
@@spacelem no worries I didn't even see it
@Qbe_Root
@Qbe_Root 4 күн бұрын
1. mostly seems wacky to Python devs but it's just a thing you can do with assignments in pretty much any other language 2. not wacky at all, actually it's pretty wacky that more languages don't allow this 3. kinda wacky but mostly just niche 4. pretty wacky on the developer's part, very normal behavior from Python (or again, any other language) 5. arguably wacky feature, I can't see a legitimate use case for lambda decorators besides IIFEs, and there could just be a named decorator for these
@Modisinamerica
@Modisinamerica 4 күн бұрын
Can you please make videos on python testing.
@boolean0z
@boolean0z 4 күн бұрын
you try to make python look like java
@Indently
@Indently 4 күн бұрын
Thank you, but I don’t deserve all the credit for what the Python devs have worked thousands of hours on
@patfre
@patfre 4 күн бұрын
If they where WHY DID THEY USE THE NAME DECIMAL FOR A FLOAT AAAAAAAAAAAAAAAA
@__christopher__
@__christopher__ Күн бұрын
@@patfre Because they are bad at naming. In particular given that Python floats are most definitely not decimal, but binary, as the following shows: >>> 0.1 + 0.2 0.30000000000000004 In a true decimal floating point type, the result would have been exactly 0.3.
@patfre
@patfre Күн бұрын
@ Well obviously its binary that’s literally what a computer uses but its still called a floating point number in this case its a 32 bit floating point number and decimal is a 128 bit floating point number
@uthoshantm
@uthoshantm 3 күн бұрын
Using this stuff should be justified by an improved readability. I would rather use an extra line than confuse by fellow developers.
@prozazo11
@prozazo11 4 күн бұрын
hello
@perplexedon9834
@perplexedon9834 3 күн бұрын
The walrus operator seems almost identical to the "if let" syntax in rust, which is one of my favourite features. In rust, functions that could fail often return an Option, whicg could either be "Some(_)" or "None". use std::collections::HashMap as dict; fn main() { let db: dict = [(0, "Bob"), (1, "James"), (2, "Sandra")].into(); let selection: i32 = 4; if let Some(user) = db.get(&selection) { println!("You selected: {user}"); } else { println!("Invalid ID {selection}: No user found"); } } I still prefer the rust version, simply because explicitly handling errors/cases through options gives much more control
@__christopher__
@__christopher__ Күн бұрын
@lambda _:_() class do_not_forget: def __enter__(self): pass def __exit__(self, *_): print("Hi, Bob!") with do_not_forget as _: print("To remain true to the video content ...")
@ayushdey5342
@ayushdey5342 3 күн бұрын
Hi, Bob!
@starryknight64
@starryknight64 3 күн бұрын
Hi, Bob!
All 39 Python Keywords Explained
34:08
Indently
Рет қаралды 209 М.
docker stack is my new favorite way to deploy to a VPS
27:47
Dreams of Code
Рет қаралды 71 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 94 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,5 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 29 МЛН
10 Crazy Python Operators That I Rarely Use
11:37
Indently
Рет қаралды 43 М.
5 Uncommon Python Features I Love
15:09
Indently
Рет қаралды 164 М.
new critical linux exploit has been hiding for 10 years.
9:32
Low Level
Рет қаралды 136 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 626 М.
New divisibility rule! (30,000 of them)
26:51
Stand-up Maths
Рет қаралды 341 М.
Creating Your Own Programming Language - Computerphile
21:15
Computerphile
Рет қаралды 115 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 216 М.
The BEST No-Nonsense VSCode Setup for Python Devs
26:05
ArjanCodes
Рет қаралды 27 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 109 М.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 94 МЛН