How to use __init__.py in (sub-)modules to define namespaces?

南楼画角 提交于 2019-11-30 15:36:40
aneroid

Is this a good python-like way to define namespaces? Or is there a better / common way to organize the file-system of a package in python. (I did not find a proper tutorial/example for this.)

This is an okay way to setup a Python package. Only okay and not 'good' because of the contents of foo.py and bar.py.

I don't like to use package.foo.foo() / package.subpackage.bar.bar() and would like to use package.foo() / package.subpackage.bar().

In this case, you can't. And not mixing the name spaces is good reason to not do from package.subpackage.bar import bar.

It would have been better if def foo(): ... and def bar(): ... were directly in __init__.py. That way, you could have done package.foo() and package.subpackage.bar(). It could also have been done by using __all__ in init but import * is also not considered good.

Or, the foo and bar packages should have had more in it like foo.function1, foo.other_func, bar.drink, etc. which gives it more human-friendly understandable organisation.

Examples and references are not within scope for good StackOverflow questions but here are some, for a well thought-out question:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!