"import foo" and "from foo import bar" 
Author Message
 "import foo" and "from foo import bar"

Quote:

> It's better to write the "from string import join" form or it's exactly
> the same (in memory use and performance) that "import string"?

There is a small performance benefit to the 'from' approach, but I
usually find that it is outweighed by the fact that it is much easier
to read and understand the code later if you don't do that.

The only time that I would recommend doing that sort of thing (other
than in modules that explicitly suggest that you do so) is if you are
going to reference that name _many_ times, such as inside a huge for-loop.

In that case, it may be worth it to get it into a local variable first.

-Justin



Fri, 29 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"

Quote:

> It's better to write the "from string import join" form or it's exactly
> the same (in memory use and performance) that "import string"?

There is a small performance benefit to the 'from' approach, but I
usually find that it is outweighed by the fact that it is much easier
to read and understand the code later if you don't do that.

The only time that I would recommend doing that sort of thing (other
than in modules that explicitly suggest that you do so) is if you are
going to reference that name _many_ times, such as inside a huge for-loop.

In that case, it may be worth it to get it into a local variable first.

-Justin



Fri, 29 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"

Quote:

> I have a question regarding the difference between those two, apart from
> having to type the module name before the symbol in the
> first case.
> If I am only using one or two simbols of a module (example: string):
> It's better to write the "from string import join" form or it's exactly
> the same (in memory use and performance) that "import string"?

There certainly is a difference, but it isn't very large. Both statements
load the 'string' module into memory, both exactly in the same way. After
they load it in, the module shows up in sys.modules. The rest depends on
the exact statement.

'import string' does something like this:

string = sys.modules['string']

Whereas 'from string import join' does something like this:

join = sys.modules['string'].join

If there is any performance difference, it's going to be in the use of
'join'. If you use 'string.join' everywhere, python will have to first look
for 'string', which will be in the global namespace, and then search that
for 'join'. If you use 'join' everywhere (either by using 'from string
import join' or by doing an explicit 'join = string.join') Python only has
to search the global namespace.

Of course, if you're that keen on performance, you probably want to pass
'join' into the functions that use it most, so that it gets turned into a
fast local variable index instead of a slow global namespace lookup ;)

Bottom line: In performance it has no real impact. Use whichever you deem
most readable: having to append a function or variable with the module name
can really clutter your code. On the other hand, by loading functions and
variables into your own namespace, you might shadow builtin functions or
variables (or worse, your own !) and cause great confusion when trying to
find out where a function came from.

--

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!



Fri, 29 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"
I have a question regarding the difference between those two, apart from
having to type the module name before the symbol in the
first case.

If I am only using one or two simbols of a module (example: string):

It's better to write the "from string import join" form or it's exactly
the same (in memory use and performance) that "import string"?



Sat, 30 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"

Quote:

> I have a question regarding the difference between those two, apart from
> having to type the module name before the symbol in the
> first case.
> If I am only using one or two simbols of a module (example: string):
> It's better to write the "from string import join" form or it's exactly
> the same (in memory use and performance) that "import string"?

There certainly is a difference, but it isn't very large. Both statements
load the 'string' module into memory, both exactly in the same way. After
they load it in, the module shows up in sys.modules. The rest depends on
the exact statement.

'import string' does something like this:

string = sys.modules['string']

Whereas 'from string import join' does something like this:

join = sys.modules['string'].join

If there is any performance difference, it's going to be in the use of
'join'. If you use 'string.join' everywhere, Python will have to first look
for 'string', which will be in the global namespace, and then search that
for 'join'. If you use 'join' everywhere (either by using 'from string
import join' or by doing an explicit 'join = string.join') Python only has
to search the global namespace.

Of course, if you're that keen on performance, you probably want to pass
'join' into the functions that use it most, so that it gets turned into a
fast local variable index instead of a slow global namespace lookup ;)

Bottom line: In performance it has no real impact. Use whichever you deem
most readable: having to append a function or variable with the module name
can really clutter your code. On the other hand, by loading functions and
variables into your own namespace, you might shadow builtin functions or
variables (or worse, your own !) and cause great confusion when trying to
find out where a function came from.

--

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!



Sat, 30 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"

Hi-

An additional difference, relevant if you are working in IDLE or
interactively and using names you have imported from a module
you may change (i.e., you are debugging it), is that if you
use

import me

then when you edit me.py, you can get the updated contents by using

reload(me)

I don't know of any similarly simple reload method if you are
explicitly loading specific names from me.py.

Peace,
Tom Loredo



Sat, 30 Nov 2002 03:00:00 GMT  
 "import foo" and "from foo import bar"
Justin Sheehy dijo sobre Re: "import foo" and "from foo import bar",
en fecha: 12 Jun 2000 21:15:57 -0400

|
|> It's better to write the "from string import join" form or it's exactly
|> the same (in memory use and performance) that "import string"?
|
|There is a small performance benefit to the 'from' approach, but I
|usually find that it is outweighed by the fact that it is much easier
|to read and understand the code later if you don't do that.

True.

|The only time that I would recommend doing that sort of thing (other
|than in modules that explicitly suggest that you do so) is if you are
|going to reference that name _many_ times, such as inside a huge for-loop.

I have followed your advice to make aliases to module symbols in a huge loop
and now my program runs 15% faster!

|In that case, it may be worth it to get it into a local variable first.

Thanks Justin and Thomas. This newsgroup amazes me increasingly, I post
the message and in 20 minutes, Wham! two good answers.



Sun, 01 Dec 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. A use for "foo = foo()"

2. $foo == "" or [llength $foo] == 0

3. "foo"

4. #pragma interface(C,"foo")

5. Dpulicating "class Foo:" in C

6. print "foo", without a space

7. "exception in foo.__del__() ignored" strangeness

8. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

9. Is "form X import *" different from "import X"?

10. "Importing From Application" with CFW 2003

11. "Importing From Application" with CFW 2003

12. "import" analysis

 

 
Powered by phpBB® Forum Software