Python Forum

Full Version: [split] Out of curiosity. Why is it bad to * import
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
snippsat Wrote:never make a package that use *
Out of curiosity. Why is it bad?
I can see that exposes all the method from the module, which aren't necessary for the context could perhaps be a security breech. Is there any other issues which I should be aware off? Performance/memory usage etc?
From Modules doc
Quote:Note that in general the practice of importing * from a module or package is frowned upon,
since it often causes poorly readable code.
However, it is okay to use it to save typing in interactive sessions.
Namespace pollution,and where dos stuff comes from Confused
Risk of overriding variables/functions etc.
Example:
from math import *

def foo(arg):
    return trunc(arg) + arg

>>> foo(3.5)
6.5
Think if you see trunc(arg) in line 250,and don't know that it's a math method.
Look at import and see from math import * that dos not help at all.
Better:
from math import trunc

def foo(arg):
    return trunc(arg) + arg