Q: import statement within a function

Tomasz Lisowski lisowski.tomasz at sssa.NOSPAM.com.pl
Fri Feb 2 08:17:03 CET 2001


This is perhaps rather a basic question, but ...

I have seen often import statements included within a function's body, not
in the global scope, as it is most common.

Approach 1:
-------------
def fun(s):
    ...
    import string
    words=string.split(s)

as compared to:

Approach 2:
-------------
import string

def fun(s):
    ...
    words=string.split(s)


What are the advantages of the first approach over the second. Won't Python
do the importing job many times, each time the function fun() is called?

Regards,
Tomasz Lisowski






More information about the Python-list mailing list