module - why would I put python code in __init__.py files -


i looking type of code put in __init__.py files , best practices related this. or, bad practice in general ?

any reference known documents explain appreciated.

libraries , frameworks use initialization code in __init__.py files neatly hide internal structure , provide uniform interface user.

let's take example of django forms module. various functions , classes in forms module defined in different files based on classification.

forms/   __init__.py   extras/     ...   fields.py   forms.py   widgets.py   ... 

now if create form, have know in file each function defined , code create contact form have (which incovenient , ugly).

 class commentform(forms.forms.form):     name = forms.fields.charfield()      url = forms.fields.urlfield()     comment = forms.fields.charfield(widget=forms.widgets.textarea)  

instead, in django can refer various widgets, forms, fields etc. directly forms namespace.

from django import forms  class commentform(forms.form):     name = forms.charfield()     url = forms.urlfield()     comment = forms.charfield(widget=forms.textarea) 

how possible? make possible, django adds following statement forms/__init__.py file import widgets, forms, fields etc. forms namespace.

from widgets import * fields import * forms import * models import * 

as can see, simplifies life when creating forms because don't have worry in each function/class defined , use of these directly forms namespace. 1 example can see examples these in other frameworks , libraries.


Comments