Another question

Bill Scherer scherbi at bam.com
Mon Apr 17 11:34:03 EDT 2000


Jeff -

The main reason (AFAIK) you don't want to do 'from <mod> import *' is to
avoid namespace clashing.
A better way is this:

#untested
import random
randint = random.randint

for i in xrange(1,1000000):
    print randint(1,10)

#

This method allows you to control exactly which names you import into
your local namespace form the module.  The above should also be a little
faster due to the use of xrange.  Se the docs for why.

-Bill

Jeff Massung wrote:

> Okay, another quick question. If it is quicker to do:
>
> import random
> from random import randint
> for i in range(1,1000000):
>     print randint(1,10)
>
> than this:
>
> import random
> for i in range(1,1000000):
>     print random.randint(1,10)
>
> Why would I not do "from <module> import *" with every module I load?
>
> Jeff
>
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list