-2

I saw a line in the python code I downloaded:

from sth.all import *

What does this .all mean? I cannot find any answer related to this.

drdot
  • 3,215
  • 9
  • 46
  • 81
  • `sth` is a package that has an `__init__.py` file, `all` is a module, `*` is everything in that module. – Maroun Jan 18 '15 at 07:14
  • 1
    By the way, just a tip: never use `import *`. Never ever. It makes code totally unreadable. That's not directly related to the question, though -- `from sth.all import foo` would raise the same question. – Andrew Gorcester Jan 18 '15 at 07:14
  • @MarounMaroun, what is a package? you mean a directory? – drdot Jan 18 '15 at 07:15
  • 1
    @dannycrane Python package is a directory, yes. – Maroun Jan 18 '15 at 07:16
  • @AndrewGorcester, thank you for the tip. Do you mean when I call the function, I will not need to specify the file where the function comes from and this may cause confusions? – drdot Jan 18 '15 at 07:19
  • 4
    When you import *, it's likely that many functions and classes are being imported. Furthermore, you don't know offhand what the names of all of these are, so they clutter your namespace. Imagine you import * and it imports a function called "test". Then later, you import * from another package, and it also has a function "test". When you go to use test, it will use one and not the other, and this can lead to very hard-to-find errors. – Dashing Adam Hughes Jan 18 '15 at 07:20
  • Danny, see the official Python docs on [packages](https://docs.python.org/2/tutorial/modules.html#packages) for some info. – PM 2Ring Jan 18 '15 at 07:22
  • @DashingAdamHughes, Thank you. I just want to make sure I understand correctly. – drdot Jan 18 '15 at 07:24
  • @PM2Ring, appreciate the link. – drdot Jan 18 '15 at 07:24
  • For sure. I still use import * way more often than necessary. Good code usually has a special variable in the files called " __ all__ ", which specifies what can be imported when the user called import *. In this way, a code might only return a few functions when you do import *, instead of returning 100 functions for a code that lacks the variable. I guess its a fitting point, since this thread is now about all the all :) For example https://github.com/scikit-image/scikit-image/blob/master/skimage/filter/rank/__init__.py#L12 – Dashing Adam Hughes Jan 18 '15 at 07:26
  • @AndrewGorcester: Certainly `import *` is evil in a normal script, but there are times when it's necessary, typically inside `__init__.py` files. And I guess it can be handy in the interactive interpreter, OTOH, even using it there does encourage bad habits. – PM 2Ring Jan 18 '15 at 07:27
  • @PM2Ring, why is it necessary to use import * in __init__.py ? – drdot Jan 18 '15 at 07:30
  • @dannycrane: Well, it's not _strictly_ necessary, but it certainly makes things simpler when you're building a package. At this stage of your Python learning you can ignore that stuff, but if you're curious and want to see an example, look at [this demo](http://stackoverflow.com/a/26623508/4014959) I posted here a few months ago. – PM 2Ring Jan 18 '15 at 07:36

1 Answers1

6

Sounds like all is the name of the module. As in they have a file called all.py in the folder sth

Dashing Adam Hughes
  • 1,522
  • 1
  • 13
  • 12