Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41871

Re: At a loss on python scoping.

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'ideally': 0.04; 'attribute': 0.07; 'found,': 0.07; 'memory.': 0.07; 'suppose': 0.07; 'method,': 0.09; 'def': 0.12; 'a()': 0.16; 'attributes.': 0.16; 'called.': 0.16; 'clear.': 0.16; 'dictionary.': 0.16; 'example?': 0.16; 'subject:loss': 0.16; 'underlying': 0.16; 'subject:python': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'references': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'file': 0.32; 'class': 0.32; 'maybe': 0.34; 'but': 0.35; 'combination': 0.36; 'in.': 0.36; 'words,': 0.36; 'shows': 0.36; 'hi,': 0.36; 'should': 0.36; 'sometimes': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'name': 0.63; 'such': 0.63; 'between': 0.67; 'received:74.208': 0.68; 'containing': 0.69; 'received:74.208.4.194': 0.84
Date Tue, 26 Mar 2013 06:07:44 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: At a loss on python scoping.
References <CAJQX3DyT53ocRcxW+1+xRWON2wzoAwEEkEO-zFTXBRm_v5KCOQ@mail.gmail.com>
In-Reply-To <CAJQX3DyT53ocRcxW+1+xRWON2wzoAwEEkEO-zFTXBRm_v5KCOQ@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:1mGWeZOixGo/kJVdnuPLTEspfi7u88cIJaGLtl3s5n0 aVAQGucNxS35m3kFEF1iNEHg6JC7rT1ldJXNyoGlwYD8ZFjJvO PH7iULUZXctWT52wfheNo3ImKEQUodk0PMrz3G/LCwIy5BQ/Rn BWj8K5dWjxSLmdWHv8NfAaBctXOf71FmletT9QCceEdPKFVvOH VUDf03IvEsU4dmJwzZBsi2g/TvWcFPpP2iGe2vnysRDNI2KslY CzY2mklUN2LiZlJnV0ELKRZOooHgfYVnOCzByFoqU8faTXqGU8 mhgXZUrCAa9EL+p85J8wHxnovrclVw811nyEAjTFUeLPLda7Q= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3728.1364292491.2939.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364292491 news.xs4all.nl 6911 [2001:888:2000:d::a6]:34947
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41871

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: At a loss on python scoping. Dave Angel <davea@davea.name> - 2013-03-26 06:07 -0400

csiph-web