Skip to content

Commit

Permalink
Add __all__
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed May 5, 2024
1 parent 9cd5084 commit ba844f0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions book/pythontricks/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,49 @@
"# Better\n",
"from module import Class1, Class2, Class3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Control What Gets Imported"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to control what gets imported when you use `from module import *` in Python?\n",
"\n",
"`__all__` lets you specify which symbols should be imported when you run `from module import *`.\n",
"\n",
"By defining `__all__` in your module, you can explicitly control what gets exposed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# mymodule.py\n",
"__all__ = ['my_function', 'MyClass']\n",
"\n",
"def my_function():\n",
" pass\n",
"\n",
"class MyClass:\n",
" pass\n",
"\n",
"def internal_helper():\n",
" pass\n",
"\n",
"# main.py\n",
"from mymodule import *\n",
"\n",
"my_function()\n",
"internal_helper() # Error"
]
}
],
"metadata": {
Expand Down

0 comments on commit ba844f0

Please sign in to comment.