473,698 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beautiful Python

I've been reading a lot of python modules lately to see how they work
and I've stumbled across something that's sort of annoying and wanted
to find out of there was a good reason behind it. In a Perl program
when you're calling other modules you'll add "use" statements at the
beginning of your script like:

use strict;
use WWW::Mechanize;
use CGI;

This seems to be the de facto standard in the Perl community but in
python it seems most of the code I look at has import statements
everywhere in the code. Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours. Being new I didn't know if
there was a performance reason for doing this or it is simply a common
habit of developers.

Dec 26 '05 #1
8 1468
Gekitsuu napisal(a):
use strict;
use WWW::Mechanize;
use CGI;

This seems to be the de facto standard in the Perl community but in
python it seems most of the code I look at has import statements
everywhere in the code. Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours. Being new I didn't know if
there was a performance reason for doing this or it is simply a common
habit of developers.


Sometimes putting import statements at the beginning is not feasible
(i.e. only when some condition has been met), as importing has some
impact on program execution (importing executes code in imported
module). This does not resemble Java imports (I don't know Perl).

--
Jarek Zgoda
http://jpa.berlios.de/
Dec 26 '05 #2
On Mon, 26 Dec 2005 02:01:07 -0800, Gekitsuu wrote:
Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours.


Maintainability is a good reason for doing imports at the beginning of
your module. But it isn't the only consideration:

- if your module only imports modules which are part of the standard
library, the maintainability argument is irrelevant (except possibly for
very special cases like "I want to back-port your module to Python 1.5,
what modules do I need?").

- Encapsulation: sometimes you want to import objects so they are local to
a function or class. If you import them at the beginning of the module,
they will be global to the module.

- Cost: importing a module may not be cheap. Why pay that cost if you
don't need to? Sometimes it makes sense to delay importing until you
know you really need it.

- Namespace pollution: when a module imports your module, it will see
everything you have imported. By delaying imports to a local scope, you
reduce the number of objects in your module namespace that get exported.

These arguments are especially strong when using the "from module import
name" form of import.

Putting all your imports together at the beginning is recommended
but not compulsory precisely because there can be good reasons for
delaying imports. Think of "always do your imports at the beginning" as a
guideline, not a law. If you are writing something like:

import A
....code...
import B
....code...
import C
....code...

then this should be re-written as:

import A
import B
import C # or just import A, B, C
....code...

But if you are writing:

import A
....code...
def foo():
import B
# module B is only used in foo
...code...

then I don't think there is anything necessarily wrong with that.
--
Steven.

Dec 26 '05 #3
On 2005-12-26 05:01:07 -0500, "Gekitsuu" <ge******@gmail .com> said:
I've been reading a lot of python modules lately to see how they work
and I've stumbled across something that's sort of annoying and wanted
to find out of there was a good reason behind it. In a Perl program
when you're calling other modules you'll add "use" statements at the
beginning of your script like:

use strict;
use WWW::Mechanize;
use CGI;

This seems to be the de facto standard in the Perl community but in
python it seems most of the code I look at has import statements
everywhere in the code. Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours. Being new I didn't know if
there was a performance reason for doing this or it is simply a common
habit of developers.


There is a crucial difference between Perl's 'use' and Python's
'import' -- when they are executed. Glossing over some details, in
Perl, a 'use' is evaulated when a .pl or .pm file is parsed, whereas in
Python import is executed when the statement is encountered in normal
program flow. One result of this is that, in Perl, this does not do
what you expect:

if ($some_conditio n) {
use SomeWierdModule ;
}

What happens is that at parse time, SomeWierdModule is loaded, ignoring
completely the if statement. This, however, works fine in Python:

if some_condition:
import some_weird_modu le

In this case, some_weird_modu le will only be imported if some_condition
holds true when program flow hits the conditional.

The net result is that in Perl, putting use statements all over the
code has no real effect, because they all get evaluated at parse time
-- you may as well toss them prettily at the top. In Python, though,
there are occasional times where you *do* want to conditionally load a
module, so it occasionally makes sense to put them in other places.
Short of situations like the conditional importing above, though, I
would be highly suspicious of code that gratuitously tosses imports in
odd places; the namespace is global, after all; may as well be explicit
upfront.

You can really see this in action with the following:

perl <<EOF
if (0) {
use SomeWeirdModule ;
}
EOF

vs

python <<EOF
if 0:
import some_weird_modu le;
EOF

The perl snippet explodes on an invalid module, whereas the python
behaves just fine.

HTH,
Chip

--
Chip Turner ct*****@pattern .net

Dec 26 '05 #4
Gekitsuu wrote:
I've been reading a lot of python modules lately to see how they work
and I've stumbled across something that's sort of annoying and wanted
to find out of there was a good reason behind it. In a Perl program
when you're calling other modules you'll add "use" statements at the
beginning of your script like:

use strict;
use WWW::Mechanize;
use CGI;

This seems to be the de facto standard in the Perl community but in
python it seems most of the code I look at has import statements
everywhere in the code. Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours. Being new I didn't know if
there was a performance reason for doing this or it is simply a common
habit of developers.

Without taking anything away from other posts responding to your
question, the first response perhaps should have been:

"Imports are always put at the top of the file, just after
any module comments and docstrings, and before module
globals and constants."

Which is the "official" Python style guide, PEP 0008, at
http://www.python.org/peps/pep-0008.html
and basically a reflection of Guido's own recommendations .

There are reasons to break almost any rule sometimes(*), but I think
you were asking whether there IS a rule -- which is an insightful,
worthy question if you've been looking at code which begs it.

-Bill
(*) PEP 0008 itself says, even before it lays out any rules,
"know when to be inconsistent".

Dec 26 '05 #5
rbt
sc****@in-spec-inc.com wrote:
Gekitsuu wrote:
I've been reading a lot of python modules lately to see how they work
and I've stumbled across something that's sort of annoying and wanted
to find out of there was a good reason behind it. In a Perl program
when you're calling other modules you'll add "use" statements at the
beginning of your script like:

use strict;
use WWW::Mechanize;
use CGI;

This seems to be the de facto standard in the Perl community but in
python it seems most of the code I look at has import statements
everywhere in the code. Is there a sound reason for putting the imports
there are are developers just loading modules in as they need them. I
own Damian Conway's book of Perl Best Practices and it seems from a
maintainability standpoint that having all the modules declared at the
beginning would make it easier for someone coming behind you to see
what other modules they need to use yours. Being new I didn't know if
there was a performance reason for doing this or it is simply a common
habit of developers.

Without taking anything away from other posts responding to your
question, the first response perhaps should have been:

"Imports are always put at the top of the file, just after
any module comments and docstrings, and before module
globals and constants."

Which is the "official" Python style guide, PEP 0008, at
http://www.python.org/peps/pep-0008.html
and basically a reflection of Guido's own recommendations .


I've always done it this way (import at the top in alphabetical order
with standard modules before add-in modules). The one exception is that
at times I use imports to make sure that certain modules are indeed
installed. For example, when I must have win32 modules I import like this:

import os
import sys
import time

try:
import win32api
except ImportError:
print "The win32 extensions must be installed!"
sys.exit()

There are reasons to break almost any rule sometimes(*), but I think
you were asking whether there IS a rule -- which is an insightful,
worthy question if you've been looking at code which begs it.

-Bill
(*) PEP 0008 itself says, even before it lays out any rules,
"know when to be inconsistent".

Dec 27 '05 #6
That is part of what I was asking but I was also hoping to hear the
common wisdom so to speak. When I posted I had considered the idea for
a bit and the situations people have mentioned were similar to the
scenario I came up with as a good time to break such a rule. My
hypothetical situation was as follows. I'm writing a new generic SQL
module and I want to make it so I only call the appropriate module for
the type of SQL server I'm talking to. Then it would make sense to
load, for instance, the mysql module and not the sqlite, postgresql,
etc. But should it be part of the PEP to include what to do in a
situation were it makes sense to break the rule? Something like if an
import needs to be in a location other than the top of the module
because of conditions determining if it will be loaded, there should be
a comment at the top of the module where the other imports are declared
stating what is loaded, why it is elsewhere, and a general idea of
where it is. Something like..

# import mysql_module
# This is imported in the useMysql() function and is only imported if
needed

I looked for a way to make a suggestion to the PEP but it wasn't
obvious to me from the link how you'd do it.

Dec 27 '05 #7
"Gekitsuu" wrote:
That is part of what I was asking but I was also hoping to hear the
common wisdom so to speak. When I posted I had considered the idea for
a bit and the situations people have mentioned were similar to the
scenario I came up with as a good time to break such a rule. My
hypothetical situation was as follows. I'm writing a new generic SQL
module and I want to make it so I only call the appropriate module for
the type of SQL server I'm talking to. Then it would make sense to
load, for instance, the mysql module and not the sqlite, postgresql,
etc. But should it be part of the PEP to include what to do in a
situation were it makes sense to break the rule? Something like if an
import needs to be in a location other than the top of the module
because of conditions determining if it will be loaded, there should be
a comment at the top of the module where the other imports are declared
stating what is loaded, why it is elsewhere, and a general idea of
where it is. Something like..

# import mysql_module
# This is imported in the useMysql() function and is only imported if
needed

I looked for a way to make a suggestion to the PEP but it wasn't
obvious to me from the link how you'd do it.


the PEP has enough silly rules as it is; no need to add even more
sillyness.

(why would anyone interested in what your useMysql function is doing
waste any time reading comments at the top of the file ? you're over-
doing things. don't do that; that's not pythonic)

</F>

Dec 27 '05 #8
On 27 Dec 2005 10:02:17 -0800, Gekitsuu <ge******@gmail .com> wrote:
My
hypothetical situation was as follows. I'm writing a new generic SQL
module and I want to make it so I only call the appropriate module for
the type of SQL server I'm talking to. Then it would make sense to
load, for instance, the mysql module and not the sqlite, postgresql,
etc.


You should take a look at the Dabo project. The dabo.db module take a
setting and imports just the required library for the chosen database.
It really isn't that difficult a concept to handle.

--

# p.d.
Dec 27 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3520
by: vinjvinj | last post by:
I haven't used an IDE in a long time but gave wing ide a try because I wanted the same development platform on Linux and Windows. I'm currently using Ultraedit and it works fine but needed something more portable as I'm moving my main platform over to Ubuntu. I first tried jedit and was reasonably happy with it but it felt slow and it did not have a native look and feel to it. It was really hard on the eyes. I was impressed! The UI...
3
1270
by: Max | last post by:
I have a friend who has been programming in C for many years, and he is a great fan of the language. However, he (and I) are about to start a python course, and he has been asking me a lot of questions. He often responds to my answers with "Urgh! Object-orientation!" and suchlike. But today we were discussing the problem of running externally-provided code (e.g. add-on modules). Neither of us knew how to do it in C, though I suggested...
1
2727
by: Tempo | last post by:
Heya. I have never used a module/script before, and the first problem I have run into is that I do not know how to install a module/script. I have downloaded Beautiful Soup, but how do I use it in one of my own programs? I know that I use an "include" statement, but do I first need to make a copy of BeautifulSoup.pyc or BeautifulSoup.py into the Python directory? Thanks in advanced for any and all help that you may provide. Many thanks.
0
679
by: Anthra Norell | last post by:
Hi, Martin, SE is a stream editor that does not introduce the overhead and complications of overkill parsing. See if it suits your needs: http://cheeseshop.python.org/pypi/SE/2.2%20beta <EAT # delete all unmatched input "~(?i)<a.*?href.*?>~==\n" # keep hrefs and add a new line
3
10716
by: cjl | last post by:
I am learning python and beautiful soup, and I'm stuck. A web page has a table that contains data I would like to scrape. The table has a unique class, so I can use: soup.find("table", {"class": "class_name"}) This isolates the table. So far, so good. Next, this table has a certain number of rows (I won't know ahead of time how many), and each row has a set number of cells (which will be constant).
8
2767
by: js | last post by:
Hi, Have you ever seen Beautiful Python code? Zope? Django? Python standard lib? or else? Please tell me what code you think it's stunning.
0
8683
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
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8904
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
8876
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...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
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();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
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.