Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, a concise rule for Python Scope resolution, from <a href="http://rads.stackoverflow.com/amzn/click/0596513984" rel="noreferrer">Learning Python, 3rd. Ed.</a>. (These rules are specific to variable names, not attributes. If you reference it without a period, these rules apply)</p> <p>LEGB Rule.</p> <p><strong>L</strong>, Local — Names assigned in any way within a function (<code>def</code> or <code>lambda</code>)), and not declared global in that function.</p> <p><strong>E</strong>, Enclosing-function locals — Name in the local scope of any and all statically enclosing functions (<code>def</code> or <code>lambda</code>), from inner to outer.</p> <p><strong>G</strong>, Global (module) — Names assigned at the top-level of a module file, or by executing a <code>global</code> statement in a <code>def</code> within the file.</p> <p><strong>B</strong>, Built-in (Python) — Names preassigned in the built-in names module : <code>open</code>,<code>range</code>,<code>SyntaxError</code>,...</p> <p>So, in the case of</p> <pre><code>code1 class Foo: code2 def spam..... code3 for code4..: code5 x() </code></pre> <p>The for loop does not have its own namespace. In LEGB order, the scopes would be </p> <p>L : local, in <code>def spam</code> (in <code>code3</code>, <code>code 4</code>, <code>code5</code>).</p> <p>E : Enclosed function, any enclosing functions (if the whole example were in another <code>def</code>)</p> <p>G : Global. Were there any <code>x</code> declared globally in the module (<code>code1</code>)?</p> <p>B : Any builtin <code>x</code> in Python.</p> <p><code>x</code> will never be found in <code>code2</code> (even in cases where you might expect it would, see <a href="https://stackoverflow.com/a/23471004/2810305">Antti's answer</a> or <a href="https://stackoverflow.com/q/13905741/2810305">here</a>).</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