Python Style Guide Questions

Ben Finney bignose+hates-spam at
Fri Jan 16 08:39:47 CET 2009

koranthala <koranthala at> writes:

> import x
> b = x.a
>              or
> from x import a
> b = a
>    I read in Learning Python that it is always better to use the
> former - especially since namespace wont be dirtied.

The namespace isn't really dirtied by ‘from foo import spam’, since
it's clear within the module where the name ‘spam’ comes from.

What dirties a namespace is ‘from foo import *’. Avoid that.

> But, doing that with PEP 8 together causes my code to look rather
> cluttered. Reason being that - PEP 8 suggests that have max line
> length = 79 chars.

Yes, this is one valid reason to change names on import. Even better,
sometimes, can be the ‘as’ clause:

    from import UncomfortablyLongNamedClass as NamedClass

    spam = NamedClass()

> So my code mostly looks like this:
> class x:
>      def y():
>           try:
>               if test:
>                   obj.filename = str(os.path.basename
> (obj1.find_next_element().\
>                                               get_file_path()))
>                   obj.modify_time = obj.filename.find_created_time()
> +  \
>                                               datetime.timedelta
> (seconds=time.find_time())
> etc .. etc..

> Almost every line requires the '\'.

In fact, they don't.

Python allows parentheses around any expression, and allows line
continuation when a bracketing syntax (parentheses, brackets, braces)
has not been closed. Combined, these allow you to break a statement
and align the line continuations nicely with standard four-space

    obj.filename = str(
    obj.modify_time = (
        + datetime.timedelta(seconds=time.find_time()))

> Also, especially since Python also uses whitespace as indentation, I
> keep confusing the block indentation with the indentation that the
> '\' causes in the next line.

That's another reason I tend to break on an existing open-parenthesis
(e.g. for a function call) where possible; it's clearer to the eye
that the statement is incomplete.

When ther isn't an easy parenthesis available, I'm not shy about
applying parentheses, as above, merely to allow line continuation.

> Could you please let me know what you guys usually do in such cases?
> Is it advisable to go for the from x import a to avoid this clutter?

Employing the existing line continuations as above, I find I don't
often need to shorten names on import. Especially I tend to avoid
changing the name of anything imported from the standard library, or
from some very widely-used code base, since I feel that would tend to
impede readability of my code.

But, on those occasions where the names are still excessively long,
yes, the import renaming can be very useful, when applied sparingly.

 \       “Everything you read in newspapers is absolutely true, except |
  `\        for that rare story of which you happen to have first-hand |
_o__)                                         knowledge.” —Erwin Knoll |
Ben Finney

More information about the Python-list mailing list