+ 1

Python: What is this craziness!!!

The code below def func(x): return s+1 print(func(s:=5)) print(func(l:=2)) How does this not give an error?????? And who in their right minds thought this was a good idea?

21st Jun 2022, 3:52 PM
Edward Finkelstein
Edward Finkelstein - avatar
10 Answers
+ 3
Looks like only manually. You can use locals() within the scope of a function and check to see if whatever variable may be in the scope already, if it is, raise an error, if not, do whatever.
21st Jun 2022, 4:35 PM
Slick
Slick - avatar
+ 5
Yes, because it's all on how the code is executed. It throws the error when the variable name is changed because the value is assigned with the walrus operator first, then the function continues to do what it will. By the time the function reaches it's call to the s variable, it has already been created and stored in global memory, which the function has access too. It checks local memory first, then global for variables. So it found the global s variable and used it. When you change 's' to 'r', then of course it will throw an error because 's' was never created!
21st Jun 2022, 4:12 PM
Slick
Slick - avatar
+ 3
It's the walrus operator. It can assign and return a value that was assigned. Look at this code that creates the variable 's' during the first call to the function while simultaniously assigning a value to 's' AND providing s's value as the argument for the x parameter in the func(x) function. I then print s and because the variable was created and value set, the value 5 is printed 2nd. The function pulls the variable 's' from global memory to return 5 + 1 each time it's called. https://code.sololearn.com/cSP9KJ1Qk65A/?ref=app
21st Jun 2022, 4:00 PM
Slick
Slick - avatar
+ 3
And just to prove why thats not an issue, I've added several empty functions to my previously posted code with calls to return undefined variables. No error. Because they are inside of a function and only throw an error if called. Edit: And why is print(func(l:=2)) not throwing an error? Because like explained, the walrus operator returns a value as well. The x parameter in that function doesn't even make a difference, it's not used within the function. The purpose was to show that you can assign a variable and provide a value in one line. That's kinda the whole point of the operator.
21st Jun 2022, 4:14 PM
Slick
Slick - avatar
+ 3
Slick ah cool! I didn’t know about that function, thanks!!!
21st Jun 2022, 4:49 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 3
Slick the example was not from a challenge, actually, it was based off of something i discovered accidentally when implementing a machine learning function. I was calling the function with a variable called X_test where the function’s parameter was X. When debugging, i noticed python made no error about using X_test instead of X within the function, so i was pretty freaked out. The walrus i just used in this example for convenience, nothing more.
23rd Jun 2022, 4:41 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 2
@Slick I know that, and that is not what I consider crazy. The crazy part, is that in the function, I am referring to a variable that has not been defined in the same scope, namely s. The only variable that should be accessible inside func is x. One sees that if you change print(func(s:=5)) to print(func(r:=5)), THEN it says NameError: name 's' is not defined And then print(func(l:=2)), which somehow doesn't give an error. There has to be some documentation about this.
21st Jun 2022, 4:05 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 2
Korkunç the Terrible Frusciante none that i can think of off the top of my head. I would usually code in OOP anyways so no need for global variables. It looks like a way to showcase the use of it in functional programming. It can be used in OOP too with a "self." added if you really want to save that extra line or 2 each time. The example provided by Edward seems like a question from a challange, not real code. It even tries to misdirect the answerer by adding in a parameter thats not even used within the function itself. The walrus is cool, but i personally don't have much use for it with how i code, but it's always nice to at least know
23rd Jun 2022, 4:25 PM
Slick
Slick - avatar
+ 1
Slick ok, that makes sense, but i still think it’s a bad idea. Isn’t there a way to disable this behavior, so that if the variable is not local to the function or explicitly stated with ‘global’ then an error is raised? Because this is not like c/c++.
21st Jun 2022, 4:24 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
Slick Wow. So this should be then possible on principle and it is cause I just tested it: s = 5 def func(x): return s print(func(4)) def func(x): return 5 print(func("whatever")) #edit: arg float to string A constant function that doesn't use its parameter. Cool. Counterintuitive but shouldn't be, how else one could have a constant function. f(x) = 0(x) + C (I mean fn() is much better but with conditionals this is cool) - - - > ***Why I'm addressing Slick: Is possible conditionals why one would use f(x) instead of f() or just to be able to use the walrus op(which is very useful, too) ? *** Wow. This question's a worth a medal or something imo and so are Slick's answers. Great question.
23rd Jun 2022, 3:50 PM
Korkunç el Gato
Korkunç el Gato - avatar