0

I have a function

def brent(n):

in a module prime. brent requires modules fractions, random

In my main module I do:

import prime # brent is found in here
import fractions # required for brent
import random # required for brent

When I call brent(n) it errors saying it can't find random. The fix is to place

import random
import fractions

INSIDE the original brent function.

Is this intended behaviour?

4

1 回答 1

1

You should (generally) import modules at the top of your own module, not inside functions.

A module import binds the imported names to the local scope, which means that if you import things inside a function, the names will only be available to that function and nothing else in your module.

于 2012-08-22T09:06:58.623 回答