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

后端 未结 9 2008

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

    I tend to use only a few members of each module, so there's a lot of

    from john import cleese
    from terry import jones, gilliam
    

    in my code. I'll import whole modules (such as os or wx) if I expect to be using most of the module and the module name is short. I'll also import whole modules if there is a name conflict or I want to remind the reader what that function is associated with.

    import michael
    import sarah
    
    import wave
    
    gov_speech = wave.open(sarah.palin.speechfile)
    parrot_sketch = wave.open(michael.palin.justresting)
    

    (I could use from wave import open as wave_open, but I figure that wave.open will be more familiar to the reader.

提交回复
热议问题