mattharrison / IllustratedPy3

Notes and issues for Illustrated Guide to Python 3
13 stars 5 forks source link

24.1 Tip #292

Closed mclaughlin closed 7 years ago

mclaughlin commented 7 years ago

When would you import a function using from versus importing a library using the import statement?

Inconsistant verb forms:

"import a function using from versus" "importing a library using the import"

"importing" --> "import"

Or better yet, you could drop "import a library using" altogether:

When would you import a function using from versus the import statement?

If you are using a couple of attributes of a library, perhaps you might want to use a from style import.

If you are only using a couple of attributes of a from a library, perhaps you might want to use a the from style import.

If you are only using a couple attributes from a library, you might want to use the from style import.

If you need access to most of the library, it is less typing to import the library.

More explicit: However if you need to access to most of the library, it is requires less typing to import the library using the import statement.

However if you need to access most of the library, it requires less typing to import the library using the import statement.

It is also a hint to you as a programmer, where the function (or class or variable) came from.

It is also a hint to you as a programmer anyone reading your code (including you), as to where the function (or class or variable) came from.

It is also a hint to anyone reading your code (including you), as to where the function (or class or variable) came from.

Altogether:

When would you import a function using from versus the import statement? If you are only using a couple attributes from a library, you might want to use the from style import. It is possible to specify multiple comma-delimited attributes in the from construct:

-and-

However if you need to access most of the library, it requires less typing to import the library using the import statement. It is also a hint to anyone reading your code (including you), as to where the function (or class or variable) came from.

mattharrison commented 7 years ago

Oct 2