[Tutor] PyMOTW: difflib

Alan Gauld alan.gauld at btinternet.com
Mon Apr 7 20:22:06 CEST 2008

"Dick Moores" <rdm at rcblue.com> wrote 

> I'm having trouble with the concept of a name in Python.
> Is a name the same as anything defined? 

Yes pretty much anything that has a name. It could be 
a variable, a class, a module, a function, anything that 
you can refer to or can refer to an object (bearing in mind 
that modules, functions and classes are objects in Python!)

Its worth spending the time reading and playing with 
this because its a fundamental concept in Python 
and once understood many of the seeming anomolies 
become clear.

I tend to think of it this way. Python does most things 
using dictionaries. The names we define in our code 
are merely keys into some semi-hidden dictionaries.
The values of the dictionaries are the things the names 
reference - values, classes, functions etc

When you import a module (import x) you reference the 
module dictionary so still need to derefence it (eg. sys.exit)
When you import from a module (from x import *) you 
copy the keys into your local dictionary so you don't 
need to dereference them.

That's almost certainly not a technically correct explanation 
of how importing really works but its conceptually how I 
think about it. :-)


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list