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


Groups > comp.lang.python > #41861

Re: At a loss on python scoping.

From Terry Reedy <tjreedy@udel.edu>
Subject Re: At a loss on python scoping.
Date 2013-03-26 03:19 -0400
References <CAJQX3DyT53ocRcxW+1+xRWON2wzoAwEEkEO-zFTXBRm_v5KCOQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3723.1364282373.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 3/26/2013 2: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?

For CPython, both the class A and the instance a have a .__dict__ 
attribute that stores names and values. But that is intended to be 
hidden and transparent  for normal usage.


-- 
Terry Jan Reedy

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


Thread

Re: At a loss on python scoping. Terry Reedy <tjreedy@udel.edu> - 2013-03-26 03:19 -0400

csiph-web