Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100921
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-12-27 17:02 -0800 |
| References | (2 earlier) <701dd0e6-a9c1-4aa9-a3a2-6607cd3f3759@googlegroups.com> <mailman.19.1451123395.11925.python-list@python.org> <661a13a1-8084-41ca-b458-297462dc3367@googlegroups.com> <0bf611ba-c4d9-4465-8d61-6a94bcee79a4@googlegroups.com> <mailman.27.1451197940.11925.python-list@python.org> |
| Message-ID | <fa863fcb-1bbf-45ba-9b70-62c408f94856@googlegroups.com> (permalink) |
| Subject | Re: A newbie quesiton: local variable in a nested funciton |
| From | jfong@ms4.hinet.net |
Chris Angelico at 2015/12/27 UTC+8 2:32:32PM wrote: > On Sun, Dec 27, 2015 at 3:11 PM, <jfong@ms4.hinet.net> wrote: > > Last night I noticed that Python does not resolve name in "def" during import, as C does in the compile/link stage, it was deferred until it was referenced (i.e. codes was executed). That's OK for Anyway codes has to be debugged sooner or later. I just have to get used to this style. > > > > But check these codes, it seems not. > > ------- > > x = 1 # a global variable > > print(x) > > > > class Test: > > x = 4 # a class attribute > > print(x) > > def func(self): > > print(x) > > > > x1 = Test() > > x1.x = 41 # a instance's attribute > > x1.func() # it's 1 but 41 was expect:-( > > -------- > > > > --Jach > > When you import this module, it runs all top-level code. So the > 'print' at the top will happen at import time. > > Among the top-level statements is a class definition. When that gets > run (to construct the class itself - distinct from instantiating it, > which happens further down), it builds a class by executing all the > statements in it, in order. That results in that value of x being > printed, and then defines a function. > > The function definition is being run at time of class construction, > and it creates a new attribute on the Test class. At that time, the > function body isn't actually executed (as you might expect). However, > it's worth noting that the function does not inherit class scope. The > unadorned name 'x' references the global. If you want to access > class-scope names from inside methods, you need to say 'self.x', which > also applies to instance attributes. That's what would do what you > expect here. > > ChrisA Yea, right, it's in a method, not a function. A stupid mistake I had made:-( Thanks for your kindly patient with me, and Happy New Year to you:-) --Jach
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-25 19:06 -0800
Re: A newbie quesiton: local variable in a nested funciton Ben Finney <ben+python@benfinney.id.au> - 2015-12-26 14:41 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 00:56 -0800
Re: A newbie quesiton: local variable in a nested funciton Ben Finney <ben+python@benfinney.id.au> - 2015-12-26 20:37 +1100
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-26 14:44 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 01:07 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-26 20:49 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 20:05 -0800
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-26 20:11 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-27 17:32 +1100
Re: A newbie quesiton: local variable in a nested funciton jfong@ms4.hinet.net - 2015-12-27 17:02 -0800
Re: A newbie quesiton: local variable in a nested funciton Chris Angelico <rosuav@gmail.com> - 2015-12-27 17:22 +1100
csiph-web