Do more imported objects affect performance

Nick Craig-Wood nick at craig-wood.com
Tue Dec 2 06:12:31 EST 2008


On Tue, Dec 02, 2008 at 11:24:29AM +0600, Taskinoor Hasan wrote:
> On Mon, Dec 1, 2008 at 8:21 PM, Filip Gruszczy?ski <gruszczy at gmail.com>wrote:
> 
> > I see. Thanks for a really good explanation, I like to know, how to do
> > things in the proper way :)
> 
> I always prefer to use import module and then use module.function. The
> reason is simple. It makes the code more readable and maintainable.

I prefer the "from module import function".  That means that if
"module" doesn't supply "function" it raises an exception at compile
time, not run time when you try to run "module.function".  It then
becomes very easy to see which functions you use from any given module
too.  It is also very slightly faster but that isn't a major
consideration.

PEP 8 endorses this style somewhat

http://www.python.org/dev/peps/pep-0008/ - see the Imports section.

[...]

  it's okay to say this though:

        from subprocess import Popen, PIPE

[...]

  When importing a class from a class-containing module, it's usually
  okay to spell this

        from myclass import MyClass
        from foo.bar.yourclass import YourClass

  If this spelling causes local name clashes, then spell them

        import myclass
        import foo.bar.yourclass

  and use "myclass.MyClass" and "foo.bar.yourclass.YourClass"

Ultimately it is a matter of taste I think!
-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list