How does Python print a variable that is out of scope
<p>I have the following function in Pyton that seems to be working:</p>

<pre><code>def test(self):
    x = -1
    # why don't I need to initialize y = 0 here?
    if (x &lt; 0):
        y = 23

    return y
</code></pre>

<p>But for this to work why don't I need to initialize variable y? I thought Python had block scope so how is this possible? </p>

<p>There is actually no block scope in python.
Variables may be local (inside of a function) or global (same for the whole scope of the program).</p>

<p>Once you've defined the variable y inside the 'if' block its value is kept for this specific function until you specifically delete it using the 'del' command, or the function exits.
From the moment y is defined in the function, it is a local variable of this function.</p>

python block