Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85480 > unrolled thread

function inclusion problem

Started byvlyamtsev@gmail.com
First post2015-02-10 15:38 -0800
Last post2015-02-27 12:11 -0800
Articles 14 — 12 participants

Back to article view | Back to comp.lang.python


Contents

  function inclusion problem vlyamtsev@gmail.com - 2015-02-10 15:38 -0800
    Re: function inclusion problem sohcahtoa82@gmail.com - 2015-02-10 15:55 -0800
    Re: function inclusion problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-11 10:57 +1100
    Re: function inclusion problem Michael Torrie <torriem@gmail.com> - 2015-02-10 17:00 -0700
    Re: function inclusion problem sohcahtoa82@gmail.com - 2015-02-10 16:02 -0800
    Re: function inclusion problem Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-10 17:02 -0700
    Re: function inclusion problem Laura Creighton <lac@openend.se> - 2015-02-11 01:06 +0100
    Re: function inclusion problem Laura Creighton <lac@openend.se> - 2015-02-11 01:16 +0100
    Re: function inclusion problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-02-10 20:52 -0500
    Re: function inclusion problem Victor L <vlyamtsev@gmail.com> - 2015-02-11 08:27 -0500
    Re: function inclusion problem Dave Angel <d@davea.name> - 2015-02-11 10:07 -0500
    Re: function inclusion problem Tim Chase <python.list@tim.thechases.com> - 2015-02-11 09:22 -0600
    Re: function inclusion problem Chris Angelico <rosuav@gmail.com> - 2015-02-12 02:37 +1100
    Re: function inclusion problem blue <catalinfest@gmail.com> - 2015-02-27 12:11 -0800

#85480 — function inclusion problem

Fromvlyamtsev@gmail.com
Date2015-02-10 15:38 -0800
Subjectfunction inclusion problem
Message-ID<d7871507-2677-4fef-9462-d429217e7ad3@googlegroups.com>
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

[toc] | [next] | [standalone]


#85482

Fromsohcahtoa82@gmail.com
Date2015-02-10 15:55 -0800
Message-ID<48ec9b8f-8340-490e-8ea4-c4c45c1e43b7@googlegroups.com>
In reply to#85480
On Tuesday, February 10, 2015 at 3:38:12 PM UTC-8, vlya...@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

It would help us help you a lot of you copy/paste your code from both mydef.py and test.py so we can see exactly what you're doing.

Don't re-type what you entered, because people (Especially new programmers) are prone to either making typos or leaving out certain things because they don't think they're important.  Copy/Paste the code from the two files and then copy/paste the error you're getting.

[toc] | [prev] | [next] | [standalone]


#85483

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-02-11 10:57 +1100
Message-ID<54da9b01$0$13013$c3e8da3$5496439d@news.astraweb.com>
In reply to#85480
vlyamtsev@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


Preferred:

import mydef
mydef.Fatalln("my test")



Also acceptable:


from mydef import Fatalln
Fatalln("my test")





-- 
Steven

[toc] | [prev] | [next] | [standalone]


#85484

FromMichael Torrie <torriem@gmail.com>
Date2015-02-10 17:00 -0700
Message-ID<mailman.18632.1423612827.18130.python-list@python.org>
In reply to#85480
On 02/10/2015 04:38 PM, vlyamtsev@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?

Almost.

Try this:

mydef.Fatalln()

Unless you import the symbols from your mydef module into your program
they have to referenced by the module name.  This is a good thing and it
helps keep your code separated and clean.  It is possible to import
individual symbols from a module like this:

from mydef import Fatalln

Avoid the temptation to import *all* symbols from a module into the
current program's namespace.  Better to type out the extra bit.
Alternatively you can alias imports like this

import somemodule.submodule as foo

Frequently this idiom is used when working with numpy to save a bit of
time, while preserving the separate namespaces.

[toc] | [prev] | [next] | [standalone]


#85485

Fromsohcahtoa82@gmail.com
Date2015-02-10 16:02 -0800
Message-ID<4a453b87-efce-40bc-b512-e51267917703@googlegroups.com>
In reply to#85480
On Tuesday, February 10, 2015 at 3:38:12 PM UTC-8, vlya...@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

[toc] | [prev] | [next] | [standalone]


#85486

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-10 17:02 -0700
Message-ID<mailman.18633.1423613005.18130.python-list@python.org>
In reply to#85480
On Tue, Feb 10, 2015 at 4:38 PM,  <vlyamtsev@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?

    import mydef
    mydef.Fatalin("my test")

or

    from mydef import Fatalin
    Fatalin("my test")

[toc] | [prev] | [next] | [standalone]


#85487

FromLaura Creighton <lac@openend.se>
Date2015-02-11 01:06 +0100
Message-ID<mailman.18634.1423613168.18130.python-list@python.org>
In reply to#85480
In a message of Tue, 10 Feb 2015 15:38:02 -0800, vlyamtsev@gmail.com writes:
>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
>-- 
>https://mail.python.org/mailman/listinfo/python-list

from mydef import Fatalln

[toc] | [prev] | [next] | [standalone]


#85491

FromLaura Creighton <lac@openend.se>
Date2015-02-11 01:16 +0100
Message-ID<mailman.18637.1423613800.18130.python-list@python.org>
In reply to#85480
In a message of Wed, 11 Feb 2015 01:06:00 +0100, Laura Creighton writes:
>In a message of Tue, 10 Feb 2015 15:38:02 -0800, vlyamtsev@gmail.com writes:
>>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
>>-- 
>>https://mail.python.org/mailman/listinfo/python-list
>
>from mydef import Fatalln
>

Also, please be warned.  If you use a unix system, or a linux
system.  There are lots of problems you can get into if you
expect something named 'test' to run your code.  Because they
already have one in their shell, and that one wins, and so ...
well, test.py is safe.  But if you rename it as a script and call
it the binary file test ...

Bad and unexpected things happen.

Name it 'testme' or something like that.  Never have that problem again.
:)

Been there, done that!
Laura

[toc] | [prev] | [next] | [standalone]


#85496

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-02-10 20:52 -0500
Message-ID<mailman.18640.1423619588.18130.python-list@python.org>
In reply to#85480
fOn Tue, 10 Feb 2015 15:38:02 -0800 (PST), vlyamtsev@gmail.com declaimed
the following:

>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

	mydef.Fatalln()

	The import only makes the module available. To use names from inside
the module you have to specify as module.name
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#85527

FromVictor L <vlyamtsev@gmail.com>
Date2015-02-11 08:27 -0500
Message-ID<mailman.18654.1423661250.18130.python-list@python.org>
In reply to#85480

[Multipart message — attachments visible in raw view] — view raw

Laura, thanks for the answer - it works. Is there some equivalent of
"include" to expose every function in that script?
Thanks again,
-V

On Tue, Feb 10, 2015 at 7:16 PM, Laura Creighton <lac@openend.se> wrote:

> In a message of Wed, 11 Feb 2015 01:06:00 +0100, Laura Creighton writes:
> >In a message of Tue, 10 Feb 2015 15:38:02 -0800, vlyamtsev@gmail.com
> writes:
> >>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
> >>--
> >>https://mail.python.org/mailman/listinfo/python-list
> >
> >from mydef import Fatalln
> >
>
> Also, please be warned.  If you use a unix system, or a linux
> system.  There are lots of problems you can get into if you
> expect something named 'test' to run your code.  Because they
> already have one in their shell, and that one wins, and so ...
> well, test.py is safe.  But if you rename it as a script and call
> it the binary file test ...
>
> Bad and unexpected things happen.
>
> Name it 'testme' or something like that.  Never have that problem again.
> :)
>
> Been there, done that!
> Laura
>
>

[toc] | [prev] | [next] | [standalone]


#85532

FromDave Angel <d@davea.name>
Date2015-02-11 10:07 -0500
Message-ID<mailman.18656.1423667239.18130.python-list@python.org>
In reply to#85480
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 sohcahtoa82@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

[toc] | [prev] | [next] | [standalone]


#85533

FromTim Chase <python.list@tim.thechases.com>
Date2015-02-11 09:22 -0600
Message-ID<mailman.18657.1423668981.18130.python-list@python.org>
In reply to#85480
On 2015-02-11 10:07, Dave Angel wrote:
> 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

or, if that's verbose, you can give a shorter alias:

  import Tkinter as tk
  root = tk.Tk()
  root.mainloop()

-tkc



[toc] | [prev] | [next] | [standalone]


#85534

FromChris Angelico <rosuav@gmail.com>
Date2015-02-12 02:37 +1100
Message-ID<mailman.18658.1423669040.18130.python-list@python.org>
In reply to#85480
On Thu, Feb 12, 2015 at 2:07 AM, Dave Angel <d@davea.name> wrote:
> 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.

Never mind about conflicts and order of importing... just try figuring
out code like this:

from os import *
from sys import *
from math import *

# Calculate the total size of all files in a directory
tot = 0
for path, dirs, files in walk(argv[1]):
    # We don't need to sum the directories separately
    for f in files:
        # getsizeof() returns a value in bytes
        tot += getsizeof(f)/1024.0/1024.0

print("Total directory size:", floor(tot), "MB")

Now, I'm sure some of the experienced Python programmers here can see
exactly what's wrong. But can everyone? I doubt it. Even if you run
it, I doubt you'd get any better clue. But if you could see which
module everything was imported from, it'd be pretty obvious.

ChrisA

[toc] | [prev] | [next] | [standalone]


#86586

Fromblue <catalinfest@gmail.com>
Date2015-02-27 12:11 -0800
Message-ID<01e7effc-7cf2-4e27-a8df-d1fc86613f01@googlegroups.com>
In reply to#85480
On Wednesday, February 11, 2015 at 1:38:12 AM UTC+2, vlya...@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

...try to set your python utf-8 encode .
and read the FAQ or python manual 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web