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

前端 未结 6 1513
醉梦人生
醉梦人生 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:33

    I believe that it's best practice (according to some PEP's) that you keep import statements at the beginning of a module. You can add import statements to an __init__.py file, which will import those module to all modules inside the package.

    So...it's certainly something you can do the way you're doing it, but it's discouraged and actually unnecessary.

提交回复
热议问题