Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29992
| References | <95e88ca6-36ad-4820-8ecc-c9a788517430@googlegroups.com> <50610671$0$29981$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2012-09-24 21:47 -0400 |
| Subject | Re: which a is used? |
| From | Dwight Hutto <dwightdhutto@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1263.1348537627.27098.python-list@python.org> (permalink) |
On Mon, Sep 24, 2012 at 9:18 PM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > On Mon, 24 Sep 2012 16:43:24 -0700, Jayden wrote: > >> Dear All, >> >> I have a simple code as follows: >> >> # Begin >> a = 1 >> >> def f(): >> print a >>Paul Rubin <no.email@nospam.invalid> >> def g(): >> a = 20 >> f() >> >> g() >> #End >> >> I think the results should be 20, but it is 1. Would you please tell me >> why? > > You are expecting "dynamic scoping", Python uses "static scoping" (or > lexical scoping). With lexical scoping, you can reason about the > behavioPaul Rubin <no.email@nospam.invalid>ur of a function by knowing only how and where it is defined. The > caller is irrelevant. > > Since fuPaul Rubin <no.email@nospam.invalid>nction f is defined globally, and does not have its own local > variable a, it will always see the global variable a no matter where it > is called. So when you call f() from inside g(), f prints 1, the global > a, not 20, g's local a. > > While dynamic scoping has its uses, it is more tricky to use correctly. > One can no longer understand the behaviour of a function just by reading > the funcPaul Rubin <no.email@nospam.invalid>tion's own code, knowing where and how it is defined. You also > need to know where it is called. A function f that works perfectly when > you call it from functions g, h, k, ... will suddenly misbehave (crash, > or worse, behave wrongly) when called from function v because v > accidentally changes some global variable that f relies on. > > This is especially a danger for Python, because built-in functions like > len, chr, ord, print (version 3 only), and many others are all global > variables. > > (Technically, they are in a third scope, the builtins, but that's > equivalent to being global.) > But within a class this is could be defined as self.x within the functions and changed, correct? class a(): def __init__(self,a): self.a = a def f(self): print self.a def g(self): self.a = 20 print self.a a = a(1) a.f() a.g() > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
which a is used? Jayden <jayden.shui@gmail.com> - 2012-09-24 16:43 -0700
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 19:57 -0400
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 20:06 -0400
Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-24 17:08 -0700
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:13 -0400
Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-24 19:14 -0700
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 22:37 -0400
Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:07 +0200
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-25 01:30 -0400
Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-25 05:48 -0700
Re: which a is used? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-25 08:44 +0100
Stop feeding the trolls (Was: which a is used?) D'Arcy Cain <darcy@druid.net> - 2012-09-25 09:53 -0400
Re: Stop feeding the trolls (Was: which a is used?) David Robinow <drobinow@gmail.com> - 2012-09-25 21:00 -0400
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-26 03:06 -0400
Re: Stop feeding the trolls (Was: which a is used?) alex23 <wuwei23@gmail.com> - 2012-09-26 17:40 -0700
Re: Stop feeding the trolls Ben Finney <ben+python@benfinney.id.au> - 2012-09-27 13:06 +1000
Re: Stop feeding the trolls Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-27 23:39 -0400
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-27 23:47 -0400
Re: Stop feeding the trolls (Was: which a is used?) Chris Angelico <rosuav@gmail.com> - 2012-09-28 14:40 +1000
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 00:50 -0400
Re: Stop feeding the trolls (Was: which a is used?) "Littlefield, Tyler" <tyler@tysdomain.com> - 2012-09-27 23:12 -0600
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 01:21 -0400
Re: Stop feeding the trolls (Was: which a is used?) rusi <rustompmody@gmail.com> - 2012-09-27 22:51 -0700
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 02:20 -0400
Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-26 03:07 -0400
Re: which a is used? "Colin J. Williams" <cjw@ncf.ca> - 2012-09-25 08:52 -0400
Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:06 +0200
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-25 01:22 -0400
Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:39 +0200
Re: which a is used? Terry Reedy <tjreedy@udel.edu> - 2012-09-25 01:05 -0400
Re: which a is used? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 01:18 +0000
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:47 -0400
Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:06 +0200
Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:50 -0400
Re: which a is used? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-09-25 11:30 +0200
csiph-web