Use 'import module' or 'from module import'?

前端 未结 19 2158
一向
一向 2020-11-21 07:47

I\'ve tried to find a comprehensive guide on whether it is best to use import module or from module import. I\'ve just started with Python and I\'m

相关标签:
19条回答
  • 2020-11-21 08:07

    There are some builtin modules that contain mostly bare functions (base64, math, os, shutil, sys, time, ...) and it is definitely a good practice to have these bare functions bound to some namespace and thus improve the readability of your code. Consider how more difficult is to understand the meaning of these functions without their namespace:

    copysign(foo, bar)
    monotonic()
    copystat(foo, bar)
    

    than when they are bound to some module:

    math.copysign(foo, bar)
    time.monotonic()
    shutil.copystat(foo, bar)
    

    Sometimes you even need the namespace to avoid conflicts between different modules (json.load vs. pickle.load)


    On the other hand there are some modules that contain mostly classes (configparser, datetime, tempfile, zipfile, ...) and many of them make their class names self-explanatory enough:

    configparser.RawConfigParser()
    datetime.DateTime()
    email.message.EmailMessage()
    tempfile.NamedTemporaryFile()
    zipfile.ZipFile()
    

    so there can be a debate whether using these classes with the additional module namespace in your code adds some new information or just lengthens the code.

    0 讨论(0)
  • 2020-11-21 08:08

    I personally always use

    from package.subpackage.subsubpackage import module
    

    and then access everything as

    module.function
    module.modulevar
    

    etc. The reason is that at the same time you have short invocation, and you clearly define the module namespace of each routine, something that is very useful if you have to search for usage of a given module in your source.

    Needless to say, do not use the import *, because it pollutes your namespace and it does not tell you where a given function comes from (from which module)

    Of course, you can run in trouble if you have the same module name for two different modules in two different packages, like

    from package1.subpackage import module
    from package2.subpackage import module
    

    in this case, of course you run into troubles, but then there's a strong hint that your package layout is flawed, and you have to rethink it.

    0 讨论(0)
  • 2020-11-21 08:08
    import module
    

    Is best when you will use many functions from the module.

    from module import function
    

    Is best when you want to avoid polluting the global namespace with all the functions and types from a module when you only need function.

    0 讨论(0)
  • 2020-11-21 08:08

    Import Module - You don't need additional efforts to fetch another thing from module. It has disadvantages such as redundant typing

    Module Import From - Less typing &More control over which items of a module can be accessed.To use a new item from the module you have to update your import statement.

    0 讨论(0)
  • 2020-11-21 08:10

    Since I am also a beginner, I will be trying to explain this in a simple way: In Python, we have three types of import statements which are:

    1. Generic imports:

    import math
    

    this type of import is my personal favorite, the only downside to this import technique is that if you need use any module's function you must use the following syntax:

    math.sqrt(4)
    

    of course, it increases the typing effort but as a beginner, it will help you to keep track of module and function associated with it, (a good text editor will reduce the typing effort significantly and is recommended).

    Typing effort can be further reduced by using this import statement:

    import math as m
    

    now, instead of using math.sqrt() you can use m.sqrt().

    2. Function imports:

    from math import sqrt
    

    this type of import is best suited if your code only needs to access single or few functions from the module, but for using any new item from the module you have to update import statement.

    3. Universal imports:

    from math import * 
    

    Although it reduces typing effort significantly but is not recommended because it will fill your code with various functions from the module and their name could conflict with the name of user-defined functions. example:

    If you have a function of your very own named sqrt and you import math, your function is safe: there is your sqrt and there is math.sqrt. If you do from math import *, however, you have a problem: namely, two different functions with the exact same name. Source: Codecademy

    0 讨论(0)
  • 2020-11-21 08:10

    My own answer to this depends mostly on first, how many different modules I'll be using. If i'm only going to use one or two, I'll often use from ... import since it makes for fewer keystrokes in the rest of the file, but if I'm going to make use of many different modules, I prefer just import because that means that each module reference is self-documenting. I can see where each symbol comes from without having to hunt around.

    Usuaully I prefer the self documenting style of plain import and only change to from.. import when the number of times I have to type the module name grows above 10 to 20, even if there's only one module being imported.

    0 讨论(0)
提交回复
热议问题