473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Which of the best choice ?

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
5 1924
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
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

"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
>>>>> "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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: BORT | last post by:
Please forgive me if this is TOO newbie-ish. I am toying with the idea of teaching my ten year old a little about programming. I started my search with something like "best FREE programming...
7
by: Murtix Van Basten | last post by:
Hi all, I will deploy a database project to an Oracle server, but I could not figure out which version of Oracle should I get. Here is my configuration: Hardware: Dell 1750 Dual Xeon 3.2Ghz,...
133
by: Jane Withnolastname | last post by:
I have a web page that uses an unordered list (<UL>) and the LH (list header) tag. I know LH is a valid tag because it is clearly defined by the W3C here:...
2
by: Glen Able | last post by:
Hello I'm currently writing some library code and I want to allow certain events to be handled by whatever app is using this library. E.g. responding to error messages by printing them/logging...
7
by: | last post by:
In the beginning we had Ini files. Later we had registery files. Now have xml files and our read-only myapp.config file. My question now, is what is the best way to store and load user and...
4
by: jaYPee | last post by:
I am new to mysql and wondering which one is the best for accessing mysql. I have read this article from mysql website: At present, one can access MySQL using the following 3 interfaces...
24
by: invitro81 | last post by:
Hello I've recently learnt python and I do love it! I congratulate all those geeks who produce this nice language; well, because I could be called a nearby newbee I've decided to improve my...
6
by: Patrick.O.Ige | last post by:
Professional ASP.NET 2.0 by Bill Evjen, et al or Programming Microsoft ASP.NET 2.0 Core Reference By Dino Esposito Any ideas? Patrick
6
by: JM | last post by:
I have never used a (content management system) CMS before but I need one for my internship as a webdeveloper. Requirements: runs on Apache, linux or unix, MySQL and PHP (maybe Windows server...
11
by: macca | last post by:
I know this is a php newsgroup so obviously most people here like using php, but I was just wondering what people's opinnions were on the most diverse/useful and friendly 'scripting' language was....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.