Skip to content

Instantly share code, notes, and snippets.

@Chrislu30604
Created December 27, 2018 05:21
Show Gist options
  • Save Chrislu30604/2e8f2897146aeac6144e9f128bf0ba97 to your computer and use it in GitHub Desktop.
Save Chrislu30604/2e8f2897146aeac6144e9f128bf0ba97 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Underscore"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python Interpreter"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"''"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
">>> 10\n",
">>> _*30"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ignore Value"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 3\n",
"1 5\n",
"a\n",
"b\n",
"c\n"
]
}
],
"source": [
"# Ignore a value when unpacking\n",
"x, _, y = (1, 2, 3) # x = 1, y = 3 \n",
"print(x, y)\n",
"# Ignore the multiple values. It is called \"Extended Unpacking\" which is available in only Python 3.x\n",
"x, *_, y = (1, 2, 3, 4, 5) # x = 1, y = 5 \n",
"print(x, y)\n",
"# ignore in enumerate\n",
"ignoreList = ['a', 'b', 'c']\n",
"\n",
"for _, i in enumerate(ignoreList):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Private Variables and Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### \\_single\\_leading\\_underscore\n",
"- From PEP-8 : \n",
"_single_leading_underscore: weak \"internal use\" indicator. E.g. from M import * does not import objects whose name starts with an underscore.\n",
"\n",
"- private method of class"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"_version = '1.0'\n",
"\n",
"class _ball():\n",
" _PI = 3.14\n",
" \n",
" def __init(self, radius):\n",
" self._radius = radius\n",
" \n",
" def getArea(self):\n",
" return self._calculate\n",
" \n",
" def _calculate(self):\n",
" return self._radius^2*_PI\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### single\\_trailing\\_underscore\\_\n",
"- To avoid conflict Python keywords"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'getListObject' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-af3e6c6b657b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetListObject\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'getListObject' is not defined"
]
}
],
"source": [
"list_ = getListObject()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### \\_\\_double\\_leading\\_underscore\n",
"- double underscore will mangle the attribute names of a class to avoid conflicts of attribute names between classes. (Namespace)\n",
"- _ClassName__method"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class _myPy1:\n",
"\tdef __init__(self):\n",
"\t\tprint('my first python myPy1')\n",
"class myPy:\n",
"\tdef __init__(self):\n",
"\t\tprint('my first python myPy')\n",
"\tdef __method(self):\n",
"\t\tprint('method in class myPy')\n",
"\n",
"myPy()._myPy__method()\n",
"_myPy1()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### \\_\\_double\\_leading\\_and\\_trailing\\_underscore\\_\\_\n",
"- This convention is used for special variables or methods (so-called “magic method”) such as__init__, __len__. These methods provides special syntactic features or does special things.\n",
"- __file__ indicates the location of Python file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" print('hi')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference \n",
"https://hackernoon.com/understanding-the-underscore-of-python-309d1a029edc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment