Which of the best choice ?

B

Ben Finney

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.
2147483647

Whereas, with (2), the symbols from 'sys' pollute the main namespace:
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.
 
S

SungSoo, Han

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.
 
A

Alex Martelli

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
 
T

Terry Reedy

SungSoo said:
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
 
J

John Hunter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top