function inclusion problem

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Tue Feb 10 19:02:53 EST 2015


On Tuesday, February 10, 2015 at 3:38:12 PM UTC-8, vlya... at gmail.com wrote:
> I defined function Fatalln in "mydef.py" and it works fine if i call it from "mydef.py", but when i try to call it from "test.py" in the same folder:
> import mydef
> ...
> Fatalln "my test"
> i have NameError: name 'Fatalln' is not defined
> I also tried include('mydef.py') with the same result...
> What is the right syntax?
> Thanks

If you only do `import mydef`, then it creates a module object called `mydef` which contains all the global members in mydef.py.  When you want to call a function from that module, you need to specify that you're calling a function from that module by putting the module name followed by a period, then the function.  For example:

mydef.Fatalln("my test")

If you wanted to be able to call Fatalln without using the module name, you could import just the Fatalln function:

from mydef import Fatalln
Fatalln("my test")

If you had a lot of functions in mydef.py and wanted to be able to access them all without that pesky module name, you could also do:

from mydef import *

However, this can often be considered a bad practice as you're polluting your global name space, though can be acceptable in specific scenarios.

For more information, check https://docs.python.org/3/tutorial/modules.html



More information about the Python-list mailing list