0

can someone please tell me what the differnce between using these two in my __init__.py in my package? And which is better to use?

__all__ = ['functions']

from functions import *
poke
  • 369,085
  • 72
  • 557
  • 602
Josiah Coad
  • 325
  • 4
  • 11
  • Start [here](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package). – vaultah Aug 16 '16 at 19:56
  • 1
    Those two lines do two different things: Setting `__all__` defines *which* members will be imported when using the `from … import *` syntax to import from that module. – poke Aug 16 '16 at 19:59
  • @poke so say my structure was main.py **this_pkg/ __init.py__ myfile.py** (inside has myfunctn) and in my '__init__.py' I would put '__all__ = ['myfile.py']' then in main.py I would put 'from this_pkg import *' then I should be able to say in main.py 'myfunctn(myarg)' but I can't – Josiah Coad Aug 16 '16 at 20:15
  • See [Can someone explain `__all__` in Python?](http://stackoverflow.com/q/44834/216074) and [What exactly does “import *” import?](http://stackoverflow.com/q/2360724/216074). – poke Aug 16 '16 at 20:31

2 Answers2

0

The difference between the possible two statements in the script bar__init__.py is what scope the sub-packages or modules under bar are imported into. If the package bar contains the sub-package named functions, the statement

from functions import *

in bar__init__.py will import the functions sub-package into the scope of the bar package, and it can be accessed using the reference

bar.functions

in Python code that imports bar. If bar__init__.py contains the code

all = [functions]

then Python code containing the code

from bar import *

will define the sub-package as functions (with no reference to bar.)

Either method can be used to reference the contents of the sub-package functions, but the syntax is different.

JDMorganArkansas
  • 123
  • 2
  • 10
-4
print(len(globals()))
import sys
print(len(globals()))
from sys import *
print(len(globals()))

OUTPUT:

8
9
67
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
vadim vaduxa
  • 221
  • 6
  • 16