Relative Imports

前端 未结 2 965
余生分开走
余生分开走 2021-01-04 05:41

I\'m reading Two Scoops Django Best Practices to make my coding style improve. I\'m in relative imports and here is the sample code to make it reusable.

Old          


        
2条回答
  •  天命终不由人
    2021-01-04 06:25

    I usually use imports like this only for one reason

    from .foo import bar
    from .other import sample
    

    The reason being If Tomorrow, my module name changes from say 'test' to 'mytest' then the code does not require a refactoring. The code works without breaking.

    Update

    All imports starting with a '.' dot, only works within that package. Cross package imports need require the whole path.

提交回复
热议问题