Import statement inside class/function definition - is it a good idea?

前端 未结 6 1499
醉梦人生
醉梦人生 2020-12-05 17:18

I created a module named util that provides classes and functions I often use in Python. Some of them need imported features. What are the pros and the cons of

6条回答
  •  失恋的感觉
    2020-12-05 17:46

    Like flying sheep's answer, I agree that the others are right, but I put imports in other places like in __init__() routines and function calls when I am DEVELOPING code. After my class or function has been tested and proven to work with the import inside of it, I normally give it its own module with the import following PEP8 guidelines. I do this because sometimes I forget to delete imports after refactoring code or removing old code with bad ideas. By keeping the imports inside the class or function under development, I am specifying its dependencies should I want to copy it elsewhere or promote it to its own module...

提交回复
热议问题