from xx import yy

Cameron Simpson cs at cskk.id.au
Thu Nov 16 16:46:15 EST 2017


On 16Nov2017 09:42, bvdp <bob at mellowood.ca> wrote:
>In my original case, I think (!!!), the problem was that I had a variable in 
>mod1.py and when I did the "from mod1 import myvarible" all was fine. Python 
>create a new local-to-the-module variable and initialized it to the value it 
>was set to in mod1. And at this point all is well. But, when mod1 changed the 
>value of myvariable the change didn't get passed to the other modules. Of 
>course, the reason for my confusion is that I'm thinking that python is using 
>pointers :) Opps.
>
>Maybe we need pointers in python <ducking>.

Thinking about this as pointers works pretty well. We call them references in 
Python because they are not (or need not be) memory addresses and C pointers 
are memory addresses. However the model is the same: an arrow from a variable 
name pointing at the place a value (the object) is stored.

Look:

  mod1.py: x = 1

making:

  mod1.x --> 1

Now consider:

  mod2.py: from mod1 import x

Making:

  mod1.x --> 1
             ^
  mod2.x ----+

both referring to the "1" object.

Now:
  mod2.py: x = 2

Making:

  mod1.x --> 1
  mod2.x --> 2

You see that mod1 is not adjusted. Versus:

  mod2.py: import mod1

Making:

  mod2.mod1 --> mod1, mod1.x --> 1

Then:

  mod2.py: mod1.x = 2

Making:

  mod2.mod1 --> mod1, mod1.x --> 2

Because you're adjusting the reference "mod1.x", _not_ the distinct reference 
"mod2.x".

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)

Draw little boxes with arrows.  It helps.       - Michael J. Eager



More information about the Python-list mailing list