0

This is sort of a best practice question. If I have a large number of custom classes, but don't want them in my main program is it acceptable to stick `from someFile import *' at the top? I know that I won't be doing anything to edit/redefine the classes, functions and variables from that file.

from someFile import *

a = someFunction()
#Other stuff

someFile would just contain various custom classes and function that I know work and I don't need to scroll past every time I'm working in the program. As long as I'm careful is there any reason to not do this?

Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54

5 Answers5

4

If you are using a lot of classes, it is usually safer to avoid the use of this syntax. Especially if you use third-party classes, because some of them may have the same methods. (eg. sin, cos, etc) and you can get strange behaviors.

In my opinion, it is acceptable to use this syntax when you provide an example of the use of your code. In this way you only source your method like that to show the functionalities in a more clear way.

Personally I try to avoid this syntax. I prefer to explicitly call the "right" class. If you don't like to write long class/modules names, try just to load them as aliases like

import LongModuleName as LM
NullUserException
  • 83,810
  • 28
  • 209
  • 234
Danduk82
  • 769
  • 1
  • 10
  • 29
  • @NullUserException : thanks for your edits, I'm not a native speaker ;) – Danduk82 Jan 27 '14 at 22:22
  • In my situation, however, I don't plan on distributing my source code or having it available for other people, I won't have any duplicate names, and I know where everything is coming from (me). My understanding of `from x import *` is that it basically takes that entire file and 'pastes' it into your code - which is how I made this file. I just cut out the 50 odd classes and put them into another file, just for readability. In this situation would you still say that `from x import *` should still be avoided, and is that just based in the fact that it is **very rarely** useful/safe? – Dan Oberlam Jan 27 '14 at 22:25
  • If you are 100% sure about what you are doing, you would not have asked this question ;) – Danduk82 Jan 27 '14 at 22:28
  • BTW I think in your case you can do what you want, provided that you are sure that you have no namespace conflicts, i.e. that any of your modules provides equally-named methods – Danduk82 Jan 27 '14 at 22:29
  • You're right, I'm not 100% sure of what I'm doing. I'm pretty sure, but it wouldn't be the first time I assumed something (incorrectly) and it bit me. If this turns out that way too I'll be sure to let you know so you can say **I told you so** :) – Dan Oberlam Jan 27 '14 at 22:31
  • What I usually do is to use short aliases, It's safer especially if you share your code with some other developer. You don't know what He/Her coud do, and which methods could be added. Sometimes it is also better when re-reading your own code: you always know where the given method comes from. Cheers – Danduk82 Jan 27 '14 at 22:37
3

http://www.python.org/dev/peps/pep-0008/#imports

According to pep-8:

There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).

Kirill Zaitsev
  • 4,511
  • 3
  • 21
  • 29
2

In my opinion, probably the most useful case of the from someFile import * syntax is when you are using the python interpreter interactively. Image you want to do some quick math:

$ python
>>> from math import *
>>> sin(4.0)

This is especially useful when using pylab and ipython to turn your ipython session into a MATLAB clone with just the line from pylab import *.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
1

One problem with from module import * is that it makes it difficult to see where different functions came from, compare:

import random
random.shuffle(...)

from random import shuffle
shuffle(...)

from random import *
shuffle(...)

Another is risk of name collision; if two modules have classes or functions with the same name, import * will shadow one with the other. With from x import y you can see right at the top that you might need from z import y as y2.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

Using from someFile import * is fine as long as you're aware you're importing everything into the namespace of that module. This could lead to clashes if you happen to be reusing names.

Consider only importing the classes and functions that you really use in that module as it's more explicit for others who may be reading the source code. It also makes it easier to figure out where a function is defined: if I see a function and can't locate an explicit import line in the same file then I'm forced to grep / search more broadly to see where the function is defined.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180