Skip to content

Instantly share code, notes, and snippets.

@ashish-ad
Last active February 14, 2022 11:50
Show Gist options
  • Save ashish-ad/8a0a4671e91f1044b78b929c2510ccda to your computer and use it in GitHub Desktop.
Save ashish-ad/8a0a4671e91f1044b78b929c2510ccda to your computer and use it in GitHub Desktop.
OOPs in python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Class Defination\n",
"A class is a blueprint of an object describing all the features of the object. Features imply that it can have some values and methods too as functionality."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"class item():\n",
" \"\"\"This is a class showing the shoping items\"\"\"\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating a objects from a class using __\"()\"__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"item01 = item()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Passing attributes by using __\".\"__ mehtod"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Phone 100 4\n"
]
}
],
"source": [
"item01.name = \"Phone\"\n",
"item01.price = 100\n",
"item01.quantity = 4\n",
"print(item01.name,item01.price,item01.quantity)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above way of passing attributes to a variable is called as hard way to passing varibles"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class item():\n",
" \"\"\"This is a class showing the shoping items\"\"\"\n",
" def __init__(self, name,price,quantity):\n",
" self.name=name\n",
" self.price=price\n",
" self.quantity = quantity\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In above code snippet the way of defining the `__init__` method inside the class is to make the class more usable when instanciating objects from the class.\n",
"- As it can be seen in above code snippet `self` keyword is used when passing the arguments to the `__init__` inside the class because it is used to pass the instance itself when creating a object from the class. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Phone 100 4\n"
]
}
],
"source": [
"item01 = item(\"Phone\", 100, 4)\n",
"\n",
"print(item01.name,item01.price,item01.quantity)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Definining methods inside the class"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class item():\n",
" \"\"\"\n",
" This is a class showing the shoping items \n",
" takes attributes such as:\n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" Name: Name of the Item \n",
" Price: Price of the Item \n",
" Quantity: Qauntity of the item \n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" \"\"\"\n",
" def __init__(self, name,price,quantity):\n",
" self.name=name\n",
" self.price=price\n",
" self.quantity = quantity\n",
"\n",
" def calculate_price(self):\n",
" return self.price*self.quantity"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"400\n"
]
}
],
"source": [
"item01 = item(\"Phone\", 100, 4)\n",
"\n",
"total_price=item01.calculate_price()\n",
"\n",
"print(total_price)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Asserting certain values in instanciating or else it should through error, using `assert` keyword."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"class item():\n",
" \n",
" \"\"\"\n",
" This is a class showing the shoping items \n",
" takes attributes such as:\n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" Name: Name of the Item [Should be a string]\n",
" Price: Price of the Item [Should be greater than zero]\n",
" Quantity: Qauntity of the item [Should be grater than zero]\n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" \"\"\"\n",
"\n",
" def __init__(self, name,price,quantity):\n",
" assert price >=0, f\"Price {price} is not greater than or equal to zero! \"\n",
" assert quantity >= 0, f\"Qauntity {quantity} is not greater or equal to zero\"\n",
" assert type(name) == str, f\"Passed value as {name} should be string not {type(name)}\"\n",
"\n",
" self.name=name\n",
" self.price=price\n",
" self.quantity = quantity\n",
"\n",
" def calculate_price(self):\n",
" return self.price*self.quantity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Thus this will only take certain values values as mentioned in assert.\n",
"- Thus passing anykind of different values may give error"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"item1 = item(\"Phone\", 100,1)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "Qauntity -3 is not greater or equal to zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb Cell 18'\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000018vscode-remote?line=0'>1</a>\u001b[0m item2 \u001b[39m=\u001b[39m item(\u001b[39m\"\u001b[39;49m\u001b[39mLaptop\u001b[39;49m\u001b[39m\"\u001b[39;49m,\u001b[39m1000\u001b[39;49m,\u001b[39m-\u001b[39;49m\u001b[39m3\u001b[39;49m)\n",
"\u001b[1;32m/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb Cell 15'\u001b[0m in \u001b[0;36mitem.__init__\u001b[0;34m(self, name, price, quantity)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000014vscode-remote?line=12'>13</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__init__\u001b[39m(\u001b[39mself\u001b[39m, name,price,quantity):\n\u001b[1;32m <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000014vscode-remote?line=13'>14</a>\u001b[0m \u001b[39massert\u001b[39;00m price \u001b[39m>\u001b[39m\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m, \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mPrice \u001b[39m\u001b[39m{\u001b[39;00mprice\u001b[39m}\u001b[39;00m\u001b[39m is not greater than or equal to zero! \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m---> <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000014vscode-remote?line=14'>15</a>\u001b[0m \u001b[39massert\u001b[39;00m quantity \u001b[39m>\u001b[39m\u001b[39m=\u001b[39m \u001b[39m0\u001b[39m, \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mQauntity \u001b[39m\u001b[39m{\u001b[39;00mquantity\u001b[39m}\u001b[39;00m\u001b[39m is not greater or equal to zero\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000014vscode-remote?line=15'>16</a>\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39mtype\u001b[39m(name) \u001b[39m==\u001b[39m \u001b[39mstr\u001b[39m, \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mPassed value as \u001b[39m\u001b[39m{\u001b[39;00mname\u001b[39m}\u001b[39;00m\u001b[39m should be string not \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mtype\u001b[39m(name)\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell://wsl%2Bubuntu-20.04/mnt/c/Users/ashis/OneDrive/Documents/GitHub/Repositories/Python-Projects/BootCamp-Projects/class_intro.ipynb#ch0000014vscode-remote?line=17'>18</a>\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mname\u001b[39m=\u001b[39mname\n",
"\u001b[0;31mAssertionError\u001b[0m: Qauntity -3 is not greater or equal to zero"
]
}
],
"source": [
"item2 = item(\"Laptop\",1000,-3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here in above as you can see that it is showing such error because we have provided error message in the assertion error"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" This is a class showing the shoping items \n",
" takes attributes such as:\n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" Name: Name of the Item [Should be a string]\n",
" Price: Price of the Item [Should be greater than zero]\n",
" Quantity: Qauntity of the item [Should be grater than zero]\n",
" +++++++++++++++++++++++++++++++++++++++++++++++++\n",
" \n"
]
}
],
"source": [
"print(item.__doc__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here in above the `__doc__` is used to see what this class doc string is "
]
}
],
"metadata": {
"interpreter": {
"hash": "7d6993cb2f9ce9a59d5d7380609d9cb5192a9dedd2735a011418ad9e827eb538"
},
"kernelspec": {
"display_name": "Python 3.9.10 64-bit",
"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.9.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment