On 02/11/2015 08:27 AM, Victor L wrote:
Laura, thanks for the answer - it works. Is there some equivalent of
"include" to expose every function in that script?
Thanks again,
-V

Please don't top-post, and please use text email, not html.  Thank you.

yes, as sohcahto...@gmail.com pointed out, you can do

from mydef import *

But this is nearly always bad practice. If there are only a few functions you need access to, you should do

from mydef import Fatalln, func2, func42

and if there are tons of them, you do NOT want to pollute your local namespace with them, and should do:

import mydef

x = mydef.func2()   # or whatever

The assumption is that when the code is in an importable module, it'll be maintained somewhat independently of the calling script/module. For example, if you're using a third party library, it could be updated without your having to rewrite your own calling code.

So what happens if the 3rd party adds a new function, and you happen to have one by the same name. If you used the import* semantics, you could suddenly have broken code, with the complaint "But I didn't change a thing."

Similarly, if you import from more than one module, and use the import* form, they could conflict with each other. And the order of importing will (usually) determine which names override which ones.

The time that it's reasonable to use import* is when the third-party library already recommends it. They should only do so if they have written their library to only expose a careful subset of the names declared, and documented all of them. And when they make new releases, they're careful to hide any new symbols unless carefully documented in the release notes, so you can manually check for interference.

Incidentally, this is also true of the standard library. There are symbols that are importable from multiple places, and sometimes they have the same meanings, sometimes they don't. An example (in Python 2.7) of the latter is os.walk and os.path.walk

When I want to use one of those functions, I spell it out:
     for dirpath, dirnames, filenames in os.walk(topname):

That way, there's no doubt in the reader's mind which one I intended.

--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to