At a loss on python scoping.

Terry Reedy tjreedy at udel.edu
Tue Mar 26 03:19:17 EDT 2013


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




More information about the Python-list mailing list