python - Defining any variable in __init__.py -
there many posts got use case of __init__.py
, use define global variable 1 of them.
want know there demerit in defining local variable in __init__.py
?
there no technical reason prevents using __init__.py declaring global variables.
nevertheless, mean of __init__.py modules define import structure of application. python documentation:
the __init__.py files required make python treat directories containing packages; done prevent directories common name, such string, unintentionally hiding valid modules occur later on module search path. in simplest case, __init__.py can empty file, can execute initialization code package or set __all__ variable, described later.
as developer, expect find in __init__.py file import layout of application itself. it's last place when i'm exploring source code.
therefore, hiding implementation in __init__.py misleading. true global variable lifecycle definition hard follow.
it's considered bad practice because breaks code readability 1 of python's guidelines.
import zen of python, tim peters beautiful better ugly. explicit better implicit. simple better complex. ... readability counts. ...
Comments
Post a Comment