Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion which a is used?

Received: by 10.66.85.225 with SMTP id k1mr1992458paz.32.1348531725531;
        Mon, 24 Sep 2012 17:08:45 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.68.212.99 with SMTP id nj3mr1480191pbc.20.1348531725468; Mon,
 24 Sep 2012 17:08:45 -0700 (PDT)
Path: a8ni4285854pbd.1!nntp.google.com!l3no9932616pbq.0!postnews.google.com!wm7g2000pbc.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 24 Sep 2012 17:08:45 -0700 (PDT)
Complaints-To: groups-abuse@google.com
Injection-Info: wm7g2000pbc.googlegroups.com; posting-host=132.234.83.56; posting-account=rYyWJQoAAACVJO77HvcyJfa3TnGYCqK_
NNTP-Posting-Host: 132.234.83.56
References: <95e88ca6-36ad-4820-8ecc-c9a788517430@googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4
 (KHTML, like Gecko) Chrome/22.0.1229.56 Safari/537.4,gzip(gfe)
Message-ID: <648f62d6-6f07-432c-b742-876cedfce950@wm7g2000pbc.googlegroups.com>
Subject: Re: which a is used?
From: alex23 <wuwe...@gmail.com>
Injection-Date: Tue, 25 Sep 2012 00:08:45 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Sep 25, 9:43=A0am, Jayden <jayden.s...@gmail.com> wrote:
> Dear All,
>
> I have a simple code as follows:
>
> # Begin
> a =3D 1
>
> def f():
> =A0 =A0 print a
>
> def g():
> =A0 =A0 a =3D 20
> =A0 =A0 f()
>
> g()
> #End
>
> I think the results should be 20, but it is 1. Would you please tell me w=
hy?

Because you don't declare 'a' in 'f', it looks for 'a' in the
surrounding scope _where it was defined_, not where it was called.
Because 'a' in the module scope is 1, you'll get 1.