in Education by
I am a beginner of python and have a question, very confusing for me. If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function? for example: def hello(x,y): good=hi(iy,ix) "then do somethings,and use the parameter'good'." return something def hi(iy,ix): "code" return good JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The scope of functions hello and hi are entirely different. They do not have any variables in common. Note that the result of calling hi(x,y) is some object. You save that object with the name good in the function hello. The variable named good in hello is a different variable, unrelated to the variable named good in the function hi. They're spelled the same, but the exist in different namespaces. To prove this, change the spelling the good variable in one of the two functions, you'll see that things still work. Edit. Follow-up: "so what should i do if i want use the result of hi function in hello function?" Nothing unusual. Look at hello closely. def hello(x,y): fordf150 = hi(y,x) "then do somethings,and use the variable 'fordf150'." return something def hi( ix, iy ): "compute some value, good." return good Some script evaluates hello( 2, 3). Python creates a new namespace for the evaluation of hello. In hello, x is bound to the object 2. Binding is done position order. In hello, y is bound to the object 3. In hello, Python evaluates the first statement, fordf150 = hi( y, x ), y is 3, x is 2. a. Python creates a new namespace for the evaluation of hi. b. In hi, ix is bound to the object 3. Binding is done position order. c. In hi, iy is bound to the object 2. d. In hi, something happens and good is bound to some object, say 3.1415926. e. In hi, a return is executed; identifying an object as the value for hi. In this case, the object is named by good and is the object 3.1415926. f. The hi namespace is discarded. good, ix and iy vanish. The object (3.1415926), however, remains as the value of evaluating hi. In hello, Python finishes the first statement, fordf150 = hi( y, x ), y is 3, x is 2. The value of hi is 3.1415926. a. fordf150 is bound to the object created by evaluating hi, 3.1415926. In hello, Python moves on to other statements. At some point something is bound to an object, say, 2.718281828459045. In hello, a return is executed; identifying an object as the value for hello. In this case, the object is named by something and is the object 2.718281828459045. The namespace is discarded. fordf150 and something vanish, as do x and y. The object (2.718281828459045), however, remains as the value of evaluating hello. Whatever program or script called hello gets the answer.

Related questions

0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am a beginner of python and have a question, very confusing for me. If I define a function ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I'm discovering the concept of "objects" in JavaScript. I'm making an RSS Parser, and I have an ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which option is true about session scope? (a) Objects are accessible only from the page in which they are created (b ... & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which option is true about session scope? (a) Objects are accessible only from the page in which they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Which of the following regarding scope is true about JAVAScript? A) Variables that have a local scope are only visible ... in your program have to be declared as global variables...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    Write about the Political system as a factor influencing India’s small Foreign Policy. Please answer the above question....
asked Aug 18, 2022 in Education by JackTerrance
0 votes
    search about some programming language on the internet make a small project about this in computer notebook Select the correct answer from above options...
asked Dec 30, 2021 in Education by JackTerrance
0 votes
    search about some programming language on the internet make a small project about this in computer notebook Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    Which of the following uses relatively small amount of data to estimate about bigger population? (a) Inferential ... and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
0 votes
    I need to know how to use the delete_channel command in discord.py may someone please post a code sample ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I need to know how to use the delete_channel command in discord.py may someone please post a code sample ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    Python's easy_install makes installing new packages extremely convenient. However, as far as I can tell, it doesn't ... or similar)? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
...