+ 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
19 Réponses
+ 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.
+ 6
What is inconvenient about first defining a function, then calling it? 🤔
+ 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()
+ 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.
+ 2
this is what im actually working on. its to make windows gui apps. HonFu
Lord Krishna
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥
https://code.sololearn.com/cFB7qfq1EFgY/?ref=app
+ 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.
+ 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.
+ 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.
+ 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.
+ 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.
0
Alright, so in that code, what stops you from moving your function definition to the top, right below the import?
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..