{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Cheat Sheet\n", "\n", "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n", "\n", "## Read It\n", "\n", "- [Website](https://www.pythoncheatsheet.org)\n", "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n", "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n", "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n", "\n", "## `__main__` Top-level script environment\n", "\n", "`__main__` is the name of the scope in which top-level code executes.\n", "A module’s **name** is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.\n", "\n", "A module can discover whether or not it is running in the main scope by checking its own `__name__`, which allows a common idiom for conditionally executing code in a module when it is run as a script or with `python -m` but not when it is imported:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if __name__ == \"__main__\":\n", " # execute only if run as a script\n", " main()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a package, the same effect can be achieved by including a **main**.py module, the contents of which will be executed when the module is run with -m.\n", "\n", "For example we are developing script which is designed to be used as module, we should do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Python program to execute function directly\n", "def add(a, b):\n", " return a+b\n", "\n", "add(10, 20) # we can test it by calling the function save it as calculate.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now if we want to use that module by importing we have to comment out our call,\n", "# Instead we can write like this in calculate.py\n", "if __name__ == \"__main__\":\n", " add(3, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import calculate\n", "\n", "calculate.add(3, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Advantages\n", "\n", "1. Every Python module has it’s `__name__` defined and if this is `__main__`, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.\n", "2. If you import this script as a module in another script, the **name** is set to the name of the script/module.\n", "3. Python files can act as either reusable modules, or as standalone programs.\n", "4. if `__name__ == “main”:` is used to execute some code only if the file was run directly, and not imported." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }