(slightly OT): Python and linux - very cool

TuxTrax TuxTrax at fortress.tuxnet.net
Thu Aug 1 20:31:42 EDT 2002


On Thu, 01 Aug 2002 22:59:34 +0100, Graham Ashton Wrote in
Steve Ballmers hair grease:

> On Thu, 01 Aug 2002 14:13:04 +0100, Roy Culley wrote:
> 
>> In article <3D492CAC.E3A37D82 at engcorp.com>,
>> 	Peter Hansen <peter at engcorp.com> writes:
>>> 
>>> "from x import *" certainly works
>>> properly, even though it's almost never a good idea (hint to newbies:
>>> don't do that!).
>> 
>> I'm a python newbie. How's about expanding on the hint. I just do (as an
>> example):
>> 
>> import os, re, sys, string
> 
> Nope, that's absolutely fine. You are only adding the four modules that
> you are importing to your current name space.
> 
> When you do "from somemodule import *" you will be importing absolutely
> everything that the module or package exposes to you. This means that you
> can very easily get difficult to find clashes between the names in
> somemodule and the names that exist in your current namespace.
> Consequently it's generally not a good idea.
> 
> For example, if you do "from os import *" you'll find that the builtin
> open() function seems to start behaving strangely (because it's been
> hidden by os.open(), which is a different function with a different
> interface).
> 
> I say "generally not a good idea" above because some modules are actually
> designed to be used this way (e.g. gtk).
> 
> There is also another more subtle side effect of "import *". If you find
> yourself importing multiple modules into a file it can quickly become
> difficult to keep track of which functions or classes are defined in which
> module.
> 
> This makes locating the docs or code for a function an order of magnitude
> more difficult, especially for other people. In other words, you're
> impacting your maintainability. What I'm getting at here is that saying
> somemodule.do_foo() is more "self documenting" than saying do_foo().
> 
> --
> Graham

Thanks Graham. that makes it alot clearer. It's a simple thing that
can be done, that can make life for the programmer and everyone else
that comes along, much easier. I can tell you that there have been
times that I have looked at old code that I wrote and couldn't make
heads or tails of what I was doing. In the olden days, I didn't have a
clue about the importance of comments and making things clear via the
programming style. As you demonstrate, somemodule.do_foo() is alot
clearer although it's not the only way of doing it.

Cheers,

Mathew

-- 
TuxTrax   (n.) An invincible, all terrain, Linux driven armored assault
vehicle that can take as much fire as it gives ;-)

Yes, I am a Penguin cult high priest. Flipper readings upon request.

ROT13 this email address to mail me: uvtuqrfregzna at lnubb.pbz




More information about the Python-list mailing list