[Tutor] Fwd: Accessing variables from other modules

Alan Gauld alan.gauld at yahoo.co.uk
Wed Sep 5 12:58:20 EDT 2018


On 05/09/18 15:06, Chip Wachob wrote:

> Okay, I think I'm starting to get a handle on the visibility of
> things.  As you said, much different than C.

Yes. The significant thing is to remember that in
Python you are importing names. In C you include
the contents of the file.

#include <stdio.h>

Lets you access everything in the stdio.h file
as if it were part of your own file.

import sys

Lets you see the sys module but not whats inside it.
To access whats inside you must use the name as
a prefix.

The Pytho import idiom

from sys import *

is similar in effect to the C style include
(although by an entirely different mechanism)
but is considered bad practice (for the same
reasons C++ has its scope operator(::))

> Even through the import Adafruit_GPIO as GPIO line exists in the
> AdafruitInit.py file, which is imported by the import AdafruitInit
> line in main.py, 

The critical conceptual error here is that the
file is not imported(*), only the name. Importing
in Python is all about visibility control

(*)In practice although the module is not
imported into your file it is executed, so any
new variables, classes and functions are created,
but their names are not visible in your code
except via the module name.

Let me revisit the different import styles
in more detail. Remember we are discussing visibility
of names not code.


##############################
import modulename

This makes modulename visible to the importing module.
Nothing inside modulename is visible. To access the
contents you must use modulename as a prefix.

x = modulename.name1

###############################
import modulename as alias

Exactly the same except that you can refer to
modulename using the (usually shorter) alias.

x = alias.name1

There are a few community standard aliases such as

import numpy as np
import tkinter as tk

But they are purely conventions, you can use
any alias you like. For those who enjoy typing
they could even do

import os as operating_system

And access the functions as

operating_system.listdir('.')

instead of

os.listdir('.')

if they really wanted to...

################################
from module import name1, name2

This imports specific names from within module.

You can now access name1 and name2 directly:

x - name1  # accesses module.name1

But it does NOT import module itself,
only name1, name2, etc. If you try

x = module.name3

You will get an error about module (not name3!)
not being recognised.


################################
from module import name1 as n1

Same as above but use the alias n1 instead of
the longer name1.

x = n1   # like x = module.name1

This is very like you doing the following:

from module import name1
n1 = name1

################################
from module import *

This makes all the names defined in module visible
within the importing module. Again it does not make
module itself visible, only the names inside.

This is considered bad practice because if you have
multiple modules containing the same name (things
like open() and write() are common then only the
last name imported will be visible and that can
lead to unexpected errors.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list