+ 3

Y Python cant have a fuction called before defined?

python is an interpreted language so when it interprets code it reads thru it entirely. so it should make sense that i can call a fuction before defining it bcz theres this 2-step process that interprets do right? where they scan twice and figure out which variable or whatever is connected to which variable etc. so why. and it happend to me where i had to call a func bfr defining (but that code it outta sololearn's league) so what should i do? can i do some name referencing thing where u name smthin x but then do x=y at sm point in code and then call y to call x conveniently? https://code.sololearn.com/cW00EbVn3LCr/?ref=app

21st May 2020, 6:33 PM
mohsin khan
mohsin khan - avatar
19 Respostas
+ 10
Unlike javascript. Python doesn't lift function definitions to the top scope. It parses the entire code for syntax errors, etc. But, execution only happens line by line, expression, statement wise. From the very 1st line. Functions don't get defined before they are called. So all definitions must occur before the function call.
21st May 2020, 6:50 PM
Lord Krishna
Lord Krishna - avatar
+ 6
What is inconvenient about first defining a function, then calling it? đŸ€”
21st May 2020, 6:44 PM
HonFu
HonFu - avatar
+ 6
mohsin khan reflecting on your example, you cannot use an undefined object in the global scope, but it is possible to wrap the function call inside another function, even if the called func is only defined afterwards. For instance, the following code works: def main(): callme("Young man!") def callme(x): print("Call me " + x + "!") main()
22nd May 2020, 4:00 AM
Tibor Santa
Tibor Santa - avatar
+ 2
𝐊𝐱𝐱𝐛𝐹 𝐆𝐡𝐚đČđšđ„, hm, what for example? import random print(random.randint(1, 6) There's a module random that contains all sorts of stuff, like function definitions. When you import it, immediately you have all the stuff available and can use it. I know, as Lord Krishna said, Javascript hoists stuff, but I never really understood why that is necessary or anything else inconvenient.
21st May 2020, 6:53 PM
HonFu
HonFu - avatar
22nd May 2020, 10:56 AM
mohsin khan
mohsin khan - avatar
+ 1
Yeah, but a user using some code of another person would import the module or package, right? So then all the stuff in it would be defined, and again there'd be no problem.
21st May 2020, 6:47 PM
HonFu
HonFu - avatar
+ 1
So yeah: '... and it happend to me where i had to call a func bfr defining (but that code it outta sololearn's league)...' mohsin khan, don't underestimate us - we have professionals here as well. You'd help us if you showed us that situation of yours, so that we understand why by any means you have to hoist.
21st May 2020, 6:59 PM
HonFu
HonFu - avatar
+ 1
Well, who knows if someone knows some crazy hack or something (I'll be reading), but afaik, in Python, everything is just executed line by line and that's it.
21st May 2020, 7:06 PM
HonFu
HonFu - avatar
+ 1
The position of the function makes no difference to that. Only the definition line will be executed (well, a bit of other stuff that doesn't concern us here). The code *in* the function body is not executed at definition time (only checked for spelling errors). So there's absolutely no problem if you just pull it to the top.
22nd May 2020, 12:00 PM
HonFu
HonFu - avatar
+ 1
Python code begins at line one and progresses to the last line chronologically. Calling a function before defining it would be equivalent to someone you never met knowing your name before you told it to him. That would be very illogical, unlike what python should be.
22nd May 2020, 2:14 PM
ultimate boomer
ultimate boomer - avatar
0
Alright, so in that code, what stops you from moving your function definition to the top, right below the import?
22nd May 2020, 11:15 AM
HonFu
HonFu - avatar
0
that label thing. i want it to be below the radio buttions. and, if u run the code. 2 buttions show up and a number below them and whenever u click on a radio button a new number generates below the orignal so keep doing that and it turnes into a long list of "1 2 1 2 1 2 1 " (veritcally) like this. so in the func i defined, i put the forget method. but bcz the label is below the function its giving error.... ps. that label variable containes the number so..
22nd May 2020, 11:23 AM
mohsin khan
mohsin khan - avatar