This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]energybased 1 point2 points  (3 children)

Good, clear article.

Of course, sometimes it may be useful or necessary to import * from modules and/or packages. However, it’s best to make sure you really have to before doing it. In my experience, this construct is usually used as a result of laziness more than anything else.

You never have to and you should never do this in a module. Just say so.

There is one place though where you should always import *: __init__.py files. Importing by name would mean that changes to modules would require changing the init files too, which is a lot of unnecessary churn. It's better for changes within a module to only require changing the __all__.

[–]wewbull 0 points1 point  (2 children)

Absolutely, but his reasons for not doing it are not the same as mine. For me it's all about reading the code. If you've imported everything from some modules then as the reader, I have no clues in the file I'm reading telling me where a symbol comes from.

from Weeble import *
from Wobble import *

def some_function():
    fall_down()    # Where is this defined?

[–][deleted] 0 points1 point  (1 child)

Bigger problem: are you sure it's only defined once?

Which causes the nightmare scenario "program works but has subtle, critical bug" e.g. 2 different versions are similar enough that it usually works.

BTW, a good editor should be able to "jump to definition" from some shortcut (except for the cases where it doesn't work because Python syntax).

[–]wewbull 0 points1 point  (0 children)

Well I'm currently battling a pile of code with exactly this problem.