By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Which of the best choice ?

P: n/a
There are two coding style when python import module.
(1) import sys
(2) from import sys (or from import *)

I prefer (1) style. Because it's easy to read, understand module in any category
..

Which prefer ?

Are there any difference of the 2 styles.
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 11 Nov 2003 15:54:50 -0800, SungSoo, Han wrote:
There are two coding style when python import module.
This is more than coding style; it has two distinct effects.
(1) import sys
Allows 'sys' module objects to be used within the 'sys' namespace.
(2) from import sys (or from import *)
Isn't syntactically correct (refers to a non-existent module called
'import', then has the non-keyword 'sys'). Assuming you mean:

from sys import *

this has the effect that all objects from the 'sys' module are available
in the default namespace.
I prefer (1) style. Because it's easy to read, understand module in
any category
More importantly, (1) doesn't pollute the main namespace; any objects
that have the same name in the main namespace are unambiguously
referenced with the 'sys' namepace.
maxint = 23
maxint 23 import sys
maxint 23 sys.maxint 2147483647

Whereas, with (2), the symbols from 'sys' pollute the main namespace:
maxint = 23
maxint 23 from sys import *
maxint

2147483647
Which prefer ?


It's usually preferable to use (1), because there is no namespace
collision, and it has the added benefit of making it unambiguous which
module the symbols come from when they are later used.

--
\ "Why, I'd horse-whip you if I had a horse." -- Groucho Marx |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2

P: n/a
SungSoo, Han wrote:
There are two coding style when python import module.
(1) import sys
(2) from import sys (or from import *)

I prefer (1) style. Because it's easy to read, understand module in any
category .

Which prefer ?


Style (1) is generally to be preferred. "from X import *" is almost
universally shunned because it fills your namespace in ways that you
can't really control. There may be a few good nice ways to use 'from'
(one is getting a single module from a package), but in general if you
were to forget the existence of the 'from' statement, in favour of
using only the 'import' statement, your Python code wouldn't suffer.
Alex

Jul 18 '05 #3

P: n/a

"SungSoo, Han" <ha***@daetoo.com> wrote in message
news:e1*************************@posting.google.co m...
There are two coding style when python import module.
(1) import sys
(2) from import sys (or from import *)

I prefer (1) style. Because it's easy to read, understand module in

any category

In some situations, I like a third choice: import math as m, for
instance. (But I would not bother to abbreviate 'sys'.) Typing 'm.'
as in 'm.sin' is quick and a small price for keeping math names
segregated.

TJR
Jul 18 '05 #4

P: n/a
>>>>> "SungSoo," == SungSoo, Han <ha***@daetoo.com> writes:

SungSoo> There are two coding style when python import module.
SungSoo> (1) import sys (2) from import sys (or from import *)

SungSoo> I prefer (1) style. Because it's easy to read,
SungSoo> understand module in any category .
While 'from mod import *' can cause splitting headaches and devious
bugs, I love

from mod import func

This allows me to easily replace functions with similar functionality
and signatures at the top level without having to search and replace
all the mod.func occurances in my code.

JDH

Jul 18 '05 #5

P: n/a
In article <Kf********************@comcast.com>,
"Terry Reedy" <tj*****@udel.edu> wrote:
In some situations, I like a third choice: import math as m, for
instance. (But I would not bother to abbreviate 'sys'.) Typing 'm.'
as in 'm.sin' is quick and a small price for keeping math names
segregated.


That's also good for choosing among modules which truly
are alternatives, as in

if md5_required:
import md5 as digest
else:
import sha as digest
....
digester = digest.new()
....

and so on.

Regards. Mel.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.