Initializing a package

To initialize a package, you place the Python code inside the package's __init__.py file. This code is then executed when the package is imported. For example, imagine that you have a package named test_package, which contains an __init__.py file and one module named test_module.py:

Initializing a package

You can place whatever code you like inside the __init__.py file, and when the package (or a module within the package) is imported for the first time, that code will be executed.

You might be wondering why you might want to do this. Initializing a module makes sense as a module contains various functions that might need to be initialized before they are used (for example, by setting global variables to an initial value). But why initialize a package, rather than just a module within that package?

The answer lies in what happens when you import a package. When you do this, anything you define in the package's __init__.py file becomes available at the package level. For example, imagine that your __init__.py file contained the following Python code:

def say_hello():
    print("hello")

Then you could access this function from your main program in the following way:

import my_package
my_package.say_hello()

You don't need to define the say_hello() function inside a module within the package for it to be easily accessed.

As a general principle, however, adding code to the __init__.py file isn't a great idea. It works, but people looking through your package's source code will expect the package's code to be defined inside modules rather than in the package initialization file. Also, there is only one __init__.py file for the whole package, which makes organizing your code within the package more difficult.

A better way of using package initialization files is to write your code in modules within the package, and then use the __init__.py file to import this code so that it is available at the package level. For example, you might implement the say_hello() function within the test_module module, and then include the following in the package's __init__.py file:

from test_package.test_module import say_hello

Programs using your package would still call the say_hello() function in exactly the same way. The only difference is that this function is now implemented as part of the test_module module, rather than being lumped inside the __init__.py file for the entire package.

This is a very useful technique, especially as your packages get more complicated and you have lots of functions, classes, and other definitions which you want to make available. By adding import statements to your package initialization file, you can write the parts of your package in whatever modules make the most sense to you, and then choose which functions, classes, and so on to make available at the package level.

One of the nice things about using __init__.py files in this way is that the various import statements tell the users of your package which functions and classes they should be using; if you haven't included a module or function in your package initialization file, then it's probably excluded for a reason.

Using import statements in a package initialization file also tells the users of your package where the various parts of a complex package are located—the __init__.py file acts as a kind of index into the package's source code.

To summarize, while you can include any Python code you like within a package's __init__.py file, it's probably best if you limit yourself to import statements, and keep your real package code elsewhere.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.10