'from X import a' versus 'import X; X.a'

后端 未结 9 2177

I\'ve seen some Python programmers use the following style fairly consistently (we\'ll call it style 1):

import some_module
# Use some_module.some_identifier in          


        
9条回答
  •  眼角桃花
    2021-02-04 15:24

    I prefer to import X and then use X.a as much as possible.

    My exception centers on the deeply nested modules in a big framework like Django. Their module names tend to get lengthy, and their examples all say from django.conf import settings to save you typing django.conf.settings.DEBUG everywhere.

    If the module name is deeply nested, then the exception is to use from X.Y.Z import a.

提交回复
热议问题