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

后端 未结 9 2013

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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 15:24

    I usually use a threshold to decide this. If I want to use a lot of things within some_module, I'll use:

    import some_module as sm
    x = sm.whatever
    

    If there's only one or two things I need:

    from some_module import whatever
    x = whatever
    

    That's assuming I don't need a whatever from some_other_module, of course.

    I tend to use the as clause on the imports so that I can reduce my typing and substitue another module quite easily in the future.

提交回复
热议问题