Pythonic way to write long import statements

Maximilian Kindshofer

What is the pythonic way to import from Models (or Forms or views) in Django?

To say it frank I bluntly do this:

from myapp.models import foo, bar, foobar, barfoo, foofoo, barbar, barfoobar, thelistgoeson, and, on, andon...

It is far longer than the maximum of 79 characters - but what is the better way to do this?

mu 無

Use parentheses to group your imports together:

from myapp.models import (foo, bar, foobar, barfoo, foofoo,
    barbar, barfoobar, thelistgoeson, and, on, and, so, on)

This is in accordance with PEP-328 Rationale for parentheses:

Currently, if you want to import a lot of names from a module or package, you have to choose one of several unpalatable options:

  • Write a long line with backslash continuations:
  • Write multiple import statements:

(import * is not an option ;-)

Instead, it should be possible to use Python's standard grouping mechanism (parentheses) to write the import statement:

This part of the proposal had BDFL approval from the beginning.

Parentheses support was added to Python 2.4.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pythonic way to write a loop

From Dev

Is there a more pythonic way to write

From Dev

Pythonic way to handle import errors

From Dev

Pythonic way of write if open is successful

From Dev

a pythonic way to write a constrain() function

From Java

Pythonic way to avoid "if x: return x" statements

From Dev

Pythonic way of handling two statements in a for loop?

From Dev

pythonic way to combine a lot of startswith statements

From Dev

Better way, of writing a Long "if" statements?

From Dev

Pythonic way to import and use data as object attributes

From Dev

How to write the function in a more pythonic way?

From Dev

What is the pythonic way to write strings to file?

From Dev

Most pythonic way to write high order function

From Dev

What is the pythonic way to write strings to file?

From Dev

How to write conditional import statements in QML?

From Dev

How to write parser which handles import statements?

From Dev

Convenient way to wrap long SQL statements in javascript

From Java

Pythonic way to create a long multi-line string

From Dev

Is there a better way to write test statements in mocha?

From Java

Is there a better way to write nested if statements in python?

From Dev

Is there a better way to write multiple OR statements in an if-statement?

From Dev

Is there a better way to write nested if statements in python?

From Dev

Is there a better way to write multiple OR statements in an if-statement?

From Dev

Which is the better way to write log statements

From Dev

Better way to write nested if-else statements

From Dev

Most pythonic way to import all objects in a module as their name in the module

From Dev

Is there are more pythonic way to write a while loop that only updates a variable?

From Dev

Most pythonic way to write a function to either pass in arguments or a tuple of arguments

From Dev

Is there are more pythonic way to write a while loop that only updates a variable?

Related Related

  1. 1

    Pythonic way to write a loop

  2. 2

    Is there a more pythonic way to write

  3. 3

    Pythonic way to handle import errors

  4. 4

    Pythonic way of write if open is successful

  5. 5

    a pythonic way to write a constrain() function

  6. 6

    Pythonic way to avoid "if x: return x" statements

  7. 7

    Pythonic way of handling two statements in a for loop?

  8. 8

    pythonic way to combine a lot of startswith statements

  9. 9

    Better way, of writing a Long "if" statements?

  10. 10

    Pythonic way to import and use data as object attributes

  11. 11

    How to write the function in a more pythonic way?

  12. 12

    What is the pythonic way to write strings to file?

  13. 13

    Most pythonic way to write high order function

  14. 14

    What is the pythonic way to write strings to file?

  15. 15

    How to write conditional import statements in QML?

  16. 16

    How to write parser which handles import statements?

  17. 17

    Convenient way to wrap long SQL statements in javascript

  18. 18

    Pythonic way to create a long multi-line string

  19. 19

    Is there a better way to write test statements in mocha?

  20. 20

    Is there a better way to write nested if statements in python?

  21. 21

    Is there a better way to write multiple OR statements in an if-statement?

  22. 22

    Is there a better way to write nested if statements in python?

  23. 23

    Is there a better way to write multiple OR statements in an if-statement?

  24. 24

    Which is the better way to write log statements

  25. 25

    Better way to write nested if-else statements

  26. 26

    Most pythonic way to import all objects in a module as their name in the module

  27. 27

    Is there are more pythonic way to write a while loop that only updates a variable?

  28. 28

    Most pythonic way to write a function to either pass in arguments or a tuple of arguments

  29. 29

    Is there are more pythonic way to write a while loop that only updates a variable?

HotTag

Archive