How can I create block-scoped variables in Python?
<p>Take the following Perl code:</p>

<pre><code>$a = 5;

if($a == 5)
{
    print $a;
    my $a = 8;
    print $a;
}

print $a;
</code></pre>

<p>This outputs <code>585</code> because <code>my $a</code> creates a new lexical variable that is scoped to the <code>if</code> block.</p>

<p>I want to do the same thing in Python, but this:</p>

<pre><code>a = 5;
b = 9;

if a == 5:        
    print "inside function", b
    b = 'apple';

print b
</code></pre>

<p>outputs</p>

<pre><code>inside function 9
apple
</code></pre>

<p>The variable <code>b</code> is overwritten inside the <code>if</code>. Is there any way to create a local variable <code>b</code> inside the <code>if</code> statement? I want to use the same variable name with local scope inside the <code>if</code>.</p>