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

后端 未结 9 2026

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:32

    I believe in newer versions of Python (2.5+? must check my facts...) you can even do:

    import some_other_module as some_module
    

    So you could still go with style 1 and swap in a different module later on.

    I think it generally maps to how much you want to clutter up your namespace. Will you just be using one or two names in the module? Or all of them (from x import * is not allways bad, just generally)?

提交回复
热议问题