473,789 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1940
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.c om> wrote in message
news:e1******** *************** **@posting.goog le.com...
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.c om> 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************ ********@comcas t.com>,
"Terry Reedy" <tj*****@udel.e du> 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
4814
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 language for kids." After MUCH clicking and high-level scanning, I am looking at Python and Forth. Both have advocates that say each is a great approach to learning computers. My programming classes were a long, long time ago in a land far, far...
7
4198
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, 2GB Ram, 3x36GB Hdd on Raid 5 Operating System: Redhat Linux 9 I will deploy only 1 database for the application. Only 1 DBA will use the Oracle server when necessary. When the database once deployed, Only 1
133
13277
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: http://www.w3.org/MarkUp/html3/bulletlists.html The problem is, when I try to validate the page at W3C, it tells me: " element "LH" undefined " My page is 4.01 Transitional, using charset windows-1252. Any ideas why this won't validate?
2
1295
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 them to a file/whatever. I can see two good options: 1) Hold a function ptr which can be set by the application to point to one of its (presumably static?) methods.
7
6578
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 machine specific settings for a .NET program? And what classes, or code do we have to do this in C#? I don't think that using the myapp.config is the best choice to store since the file might be read-only if the program is started from a CD ROM, or...
4
1671
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 supported by MySQL. The ODBC.NET Solution - MyODBC Driver. Using MySQL Native .NET Providers. Using the OLEDB.NET Solution - MyOLDDB Provider
24
2873
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 abilities by writing my own nice editor with python; so I've to choose among all those GUI toolkit's available there.. But I've no idea which one I should use to start with.. I've read that tkinter seems to be the de facto standart in the pyhon...
6
1230
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
3152
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 and IIS) Authentication (not sure yet): existing user database, LDAP or permissions on directories (not sure if that last one is possible) Purpose: scientist working on projects should be able to upload their
11
12125
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. I like php and have made my choice to use it as my main language, only the more I look into other languages, the more i see things that I like in them that I dont necesarrily see in PHP. For example, the more I use php I realize that php has...
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.