[Python-bugs-list] dir function anomoly (PR#174)

Tim Peters tim_one@email.msn.com
Sun, 9 Jan 2000 20:07:44 -0500


[Dennis McCoy]
[removing comments cuz mailer is breaking the lines in horrid
 places]
> # div.py
> def remainder(a,b):
> 	q = a/b
> 	r = a - q*b
> 	return r
>
> def divide(a,b):
> 	q = a/b
> 	r = a - q*b
> 	return (q,r)
>
> Wrote a script that imports all the functions in div.py
>
> # bug2.py
>
> from div import *
>
> x = dir()
> print x
>
> x = dir(div)
> print x
>
> The first call for dir() works as expected and reports both of
> the functions in the imported file div.py
>
> The second call for dir(div) generates the following Python
> error:
> NameError: div

Dennis, sorry, but this isn't a bug!  It's behaving as designed and as
documented.  "from div import *" does not import div, which is why you get a
later NameError on div.  If you want the name div to be visible, you need to
do "import div".  "from xxx import yyy, zzz" never binds xxx, only yyy and
zzz.

For more info, review the docs.

Note that it's *generally* a bad idea to use the "import *" form at all
(that's meant as a convenience for interactive mode, and for modules like
Tkinter that export very broad interfaces with unlikely-to-conflict names).