HomeAbout

__init__

In Directory

Any folder that contains __init___ becomes a Python package.

__init___ serves to:

  • Initialize package-level variables.
  • point where external locations can import submodules or contents from.
  • Avoids circular imports.

Meaning, you should place __init__.py in a directory to make it a package, allowing you to import modules from that directory.

# myapp/routes/__init__.py from .routes import users from .routes import products from .routes import orders # external from myapp.routes import users

In Class

__init__ is a constructor in a python class:

class MyClass: def __init__(self, name): self.name = name
AboutContact