-2

you guys can import like this

 from .models import *

or like this

from .models import Student
Jamil Noyda
  • 3,321
  • 2
  • 21
  • 26
  • 1
    1 or 2 could litter namespace. Use 3 or 4 depending on situation. 4 if you want to keep the imported objects inside the module namespace for better readability. 3 is fine for things like specific class names like models. The 3 and 4 examples are pretty good for the respective cases – Ramkishore M Apr 17 '18 at 05:01

2 Answers2

2

From PEP8: Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. 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).

Generally you want to import only what you need, and make sure imports are explicit (e.g. the third option in your list, but sometimes also the fourth option).

ZaxR
  • 4,896
  • 4
  • 23
  • 42
1

In general it depends on what you are trying to do and what your style of programming is. Though they all may work, I would suggest sticking with the more explicit styles such what you have listed as (3) and (4).

The problem with using the * to import everything at once is that they are prone to namespace errors and it is unclear what modules you are actually using.

Though it seems like more work it will lead to better programs that are easier to follow if you stick with importing only what you need as you need it.

fhorrobin
  • 362
  • 1
  • 10