importing question ?

Martyn Quick mrq at for.mat.bham.ac.uk
Thu Nov 28 09:55:43 EST 2002


On Thu, 28 Nov 2002, Jonas Geiregat wrote:

>  >>> import classes
>  >>> a = veryBigSnake()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> NameError: name 'veryBigSnake' is not defined
>  >>> from classes import *
>  >>> a = veryBigSnake()
> 
> why doesn't the first import work ?
> and the second does
> 
> and what is the difference between those two ?

With the first you are importing the command in the form
classes.veryBigSnake, so the following would work:

import classes
a = classes.veryBigSnake()

and would give the same as

from classes import *
a = veryBigSnake()

The difference is that the second hides where veryBigSnake comes from and
can cause confusion if you were to change the meaning of the function in
the middle of your code.  With the first you're less likely to do this.

Experts can explain this in the proper technical language better than I
can.  ;-)

Cheers,
Martyn

--------------------------------------------------------
Dr. Martyn Quick  (Research Fellow in Pure Mathematics)
University of Birmingham, Edgbaston, Birmingham, UK.
http://www.mat.bham.ac.uk/M.R.Quick/




More information about the Python-list mailing list