[Tutor] Accessing variables from other modules
Steven D'Aprano
steve at pearwood.info
Tue Sep 4 13:19:56 EDT 2018
Hi Chip, and welcome!
On Tue, Sep 04, 2018 at 11:10:36AM -0400, Chip Wachob wrote:
> I'll refrain from posting a bunch of code, but here is the 5000 foot view:
Not posting a mountain of code is a great idea, but from 5000 ft away
we can't see what is going on.
Try posting *a little bit of code*. This is written for Java programmers
but the principle is the same for Python:
http://sscce.org/
If I had to make a *wild guess* as to what is going on, it would be that
you have a couple of modules like this:
# Module spam.py
x = 1 # global variable
def foo():
print(x)
# Module eggs.py
import spam
from spam import x
foo() # prints 1, as you expect
x = 999
foo() # still prints 1, instead of 999
Is that what is going on? On something different?
A few more comments:
> execution looks like this:
>
> $ sudo python main.py
Do you really need this to be run with root permissions?
> So, the important questions are:
>
> - Was I mislead by the fact that there was a power cycle on the
> machine and it has now forgotten something that was staying resident
> when I tested the code on Friday? Or, does each 'run' of the script
> start fresh?
Um, yes no maybe?
Was there a power cycle? How were you running the scripts?
> - What approach do I need to use to be able to initialize the
> interface in spi.py and have it be something that is accessible to all
> the other modules in my project?
>
> Thank you in advance for your time.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list