Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload