Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41880 > unrolled thread
| Started by | Shiyao Ma <i@introo.me> |
|---|---|
| First post | 2013-03-26 19:14 +0800 |
| Last post | 2013-03-26 19:14 +0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: At a loss on python scoping. Shiyao Ma <i@introo.me> - 2013-03-26 19:14 +0800
| From | Shiyao Ma <i@introo.me> |
|---|---|
| Date | 2013-03-26 19:14 +0800 |
| Subject | Re: At a loss on python scoping. |
| Message-ID | <mailman.3734.1364296466.2939.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
After read Dave's answer, I think I confused LEGB with attribute lookup. So, a.r has nothing to do with LEGB. On Tue, Mar 26, 2013 at 7:03 PM, Shiyao Ma <i@introo.me> wrote: > Thx, really a nice and detailed explanation. > > > On Tue, Mar 26, 2013 at 6:07 PM, Dave Angel <davea@davea.name> wrote: > >> On 03/26/2013 02:17 AM, Shiyao Ma wrote: >> >>> Hi, >>> suppose I have a file like this: >>> class A: >>> r = 5 >>> def func(self, s): >>> self.s = s >>> a = A() >>> print(a.r) # this should print 5, but where does py store the name of >>> r >>> >>> a.func(3) >>> print(a.s) # this should print 3, also where does py store this name. >>> what's the underlying difference between the above example? >>> >>> >> I don't think this is a scoping question at all. These references are >> fully qualified, so scoping doesn't enter in. >> >> The class A has a dictionary containing the names of r and func. These >> are class attributes. Each instance has a dictionary which will contain >> the name s AFTER the A.func() is called. Ideally such an attribute will be >> assigned in the __init__() method, in which case every instance will have s >> in its dictionary. >> >> When you use a.qqq the attribute qqq is searched for in the instance >> dictionary and, if not found, in the class dictionary. If still not found, >> in the parent classes' dictionary(s). >> >> You can use dir(A) and dir(a) to look at these dictionaries, but it shows >> you the combination of them, so it's not as clear. In other words, dir(a) >> shows you both dictionaries, merged. (Seems to me dir also sometimes >> censors some of the names, but that's a vague memory. It's never left out >> anything I cared about, so maybe it's things like single-underscore names, >> or maybe just a poor memory.) >> >> >> -- >> DaveA >> -- >> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list> >> > > > > -- > My gpg pubring is available via: gpg --keyserver subkeys.pgp.net--recv-keys 307CF736 > > More on: http://about.me/introom > > -- My gpg pubring is available via: gpg --keyserver subkeys.pgp.net--recv-keys 307CF736 More on: http://about.me/introom
Back to top | Article view | comp.lang.python
csiph-web