Efficiency/style issues of import <module> vs. from <module> import <name>, ...

Stephen Hansen me+list/python at ixokai.io
Thu Jun 17 12:58:20 EDT 2010


On 6/17/10 10:01 AM, Ethan Furman wrote:
> Stephen Hansen wrote:
>> On 6/17/10 9:12 AM, python at bdurham.com wrote:
>>
>> Now, this is all IMHO: the style guide does not define any 'guidelines'
>> on this, except that its okay to use "from ... import ..." to pull in
>> classes and (implicitly) constants, and despite how the rules say 'one
>> module per line' its OK to pull in more then one name -from- a module at
>> once.
> 
> What do you mean by "(implicitly) constants"?

Quote, PEP-8:

 - Imports should usually be on separate lines, e.g.:

        Yes: import os
             import sys

        No:  import sys, os

      it's okay to say this though:

        from subprocess import Popen, PIPE

It explicitly states later its entirely OK to import classes. It never
says anything else directly, except in the example given, it shows you
importing a constant. So, its giving implicit approval to that without
really directly saying anything about it.

>>> My understanding is that both forms of the import command require
>>> the entire module to be processed.
>>
>> Yes.
>>
>> "from <module> import <name>" is just a shortcut for:
>>
>> import <module>
>> <name> = <module>.<name>
> 
> 
> There should also be a third line:
>   del <module>

You're right, I missed that.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100617/03823eb9/attachment-0001.sig>


More information about the Python-list mailing list