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.
@Indently4 күн бұрын
That's good to know!
@maleldil14 күн бұрын
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_nines4 күн бұрын
@@maleldil1 yes IIFE makes the function deliberately unusable again. it frees up the memory used for its object
@Indently3 күн бұрын
I personally didn’t know about the memory part in Python, I appreciate you sharing.
@two_number_nines3 күн бұрын
@@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
@maleldil14 күн бұрын
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.
@TAP7a3 күн бұрын
“List of list of string… oh I think I’m high” The programming experience in a single moment
@DeLittleCat4 күн бұрын
The walrus operator is also pretty useful for list comprehension.
@sahasananth9873 күн бұрын
How
@chary940721 сағат бұрын
@@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() ]
@amaarquadri4 күн бұрын
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.
@gurupartapkhalsa65653 күн бұрын
@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
@pfizerpflanze4 күн бұрын
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!
@vytah4 күн бұрын
It doesn't, floats cause a TypeError
@ConsciousAtomsКүн бұрын
To decrement x, you simply swap the operators: ~-x results in x - 1 (if x is an integer).
@islandcave87383 күн бұрын
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.
@RadenVijaya22 сағат бұрын
Walrus, reminds me to Turbo Pascal 😂
@jamesarthurkimbell23 сағат бұрын
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.
@adryelbarros32504 күн бұрын
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_173 күн бұрын
Does the LET keyword solve this problem?
@DovRine3 күн бұрын
@@b_dawg_17 partially. the module pattern solves this problem
@trinhvanquan84432 күн бұрын
@@b_dawg_17 let doesn’t solve all the problems. IIFE not only isolate variable but also function itself
@victorsago2 күн бұрын
It made sense in old JS, which lacked block scoping. But what value is there in introducing this in Python?
@patfre4 күн бұрын
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Күн бұрын
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Күн бұрын
It’s just so python doesn’t have to guess your type, it’s not necessary
@patfre4 күн бұрын
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
@yahyasalimi32543 күн бұрын
Time to confuse my colleagues
@asagiai49654 күн бұрын
So the walrus operator here acts like in-line assignment operator I don't think they are really that wacky.
@vogonjelc4 күн бұрын
The old pascal assignment operator.
@jamesross10034 күн бұрын
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.
@y2ksw12 күн бұрын
The matrix operator is particularly useful!
@negative-exampleКүн бұрын
what font do you use? looks very good!
@HubertNafalski4 күн бұрын
Hi, Bob!
@DrDeuteron4 күн бұрын
your matmul should return: type(self)(zip(*[[sum(map(operator.mul, col, row)) for col in self] for row in zip(*other)]))
@brunopanizzi4 күн бұрын
So pythonic and readable 😍😍
@DrDeuteron4 күн бұрын
@@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.
@DrDeuteron4 күн бұрын
@@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Күн бұрын
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.
@E2EOnpremUser13 сағат бұрын
instead of @lambda you could also use @dataclass and put the variables into a class with frozen=true
@Indently13 сағат бұрын
It doesn’t call itself though, which is the magic of the cursed lambda decorator.
@E2EOnpremUser10 сағат бұрын
@@Indently also class variables are declared when a class is being constructed, only once
@dmitryutkin98643 күн бұрын
It was interesting!
@terrawest95002 күн бұрын
I just learned that Python got typing like GDscript (I've been using python for tools for years)
@mrab42223 күн бұрын
10:21 There's also the opposite that subtracts 1: ~-value
@Winter0192Күн бұрын
11:21
@dipeshsamrawat79574 күн бұрын
Thank you 😊
@landsgevaer4 күн бұрын
More importantly though, is it two eyes and two tusks, or two nostrils and two tusks? (8:=
@AbhishekVaid21 сағат бұрын
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.
@75hilmar2 күн бұрын
Reminds me of Haskell
@fppasquier3 күн бұрын
Hi Bob!
@koj3sama4 күн бұрын
Walrus só much poweful 😮
@senge13373 күн бұрын
Sandra? Where is Luigi? 😢
@IndentlyКүн бұрын
Can’t be too careful with Nintendo trademarks these days, Sandra has stepped in
@DrDeuteron4 күн бұрын
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.
@MrShadowThief4 күн бұрын
Wacky principle. Walrus operator, ++n and n++ are great.
@sectorgamma4 күн бұрын
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.
@DrDeuteron4 күн бұрын
@@MrShadowThief OK, you tell Guido his principles are wacky.
@DavideCanton4 күн бұрын
@@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.
@vytah3 күн бұрын
you can call a function in an expression and that function can do assignments, so this principle has been violated since always
@alipkamate15514 күн бұрын
@ symbols that's what I use in all my machine learning code to matrix multiplication nice to see here 😊
@Davo88993 күн бұрын
Only 5?
@jadencorr68973 күн бұрын
Please, never use lambda annotation for the sake of gods
@spacelem4 күн бұрын
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Күн бұрын
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Күн бұрын
@@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Күн бұрын
@@gdclemo sorry for the double post there, I thought it had deleted the first attempt, but then it came back.
@gdclemoКүн бұрын
@@spacelem no worries I didn't even see it
@Qbe_Root4 күн бұрын
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
@Modisinamerica4 күн бұрын
Can you please make videos on python testing.
@boolean0z4 күн бұрын
you try to make python look like java
@Indently4 күн бұрын
Thank you, but I don’t deserve all the credit for what the Python devs have worked thousands of hours on
@patfre4 күн бұрын
If they where WHY DID THEY USE THE NAME DECIMAL FOR A FLOAT AAAAAAAAAAAAAAAA
@__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Күн бұрын
@ 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
@uthoshantm3 күн бұрын
Using this stuff should be justified by an improved readability. I would rather use an extra line than confuse by fellow developers.
@prozazo114 күн бұрын
hello
@perplexedon98343 күн бұрын
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__Күн бұрын
@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 ...")