Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I put code in __init__.py files?

Tags:

python

module

I am looking for what type of code would I put in __init__.py files and what are the best practices related to this. Or, is it a bad practice in general ?

Any reference to known documents that explain this is also very much appreciated.

like image 546
webDevelSouth Avatar asked Apr 29 '11 10:04

webDevelSouth


People also ask

Should you put code in init py?

So it's normally a good place to put any package-level initialisation code. The bottom line is: all names assigned in __init__.py , be it imported modules, functions or classes, are automatically available in the package namespace whenever you import the package or a module in the package.

What does __ init __ py mean?

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

What do I put in Python init?

What to put in __init__.py. There is a range of options of what to put in an __init__.py file. The most minimal thing to do is to leave the __init__.py file totally empty. Moving slightly away from this, while still keeping things simple, you can use an __init__.py only for determining import order.

What is the meaning of the presence of __ init __ py file in the main directory?

If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. You can use this to execute package initialization code, for example for the initialization of package-level data.


1 Answers

Libraries and frameworks usually use initialization code in __init__.py files to neatly hide internal structure and provide a uniform interface to the user.

Let's take the example of Django forms module. Various functions and classes in forms module are defined in different files based on their classification.

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

Now if you were to create a form, you would have to know in which file each function is defined and your code to create a contact form will have to look something like this (which is incovenient and 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 you can just refer to various widgets, forms, fields etc. directly from the forms namespace.

from django import forms  class CommentForm(forms.Form):     name = forms.CharField()     url = forms.URLField()     comment = forms.CharField(widget=forms.Textarea) 

How is this possible? To make this possible, Django adds the following statement to forms/__init__.py file which import all the widgets, forms, fields etc. into the forms namespace.

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

As you can see, this simplifies your life when creating the forms because now you don't have to worry about in where each function/class is defined and just use all of these directly from forms namespace. This is just one example but you can see examples like these in other frameworks and libraries.

like image 161
Praveen Gollakota Avatar answered Sep 21 '22 07:09

Praveen Gollakota