Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 63a27c2

Browse filesBrowse files
committed
OOP part i
1 parent 24f4d9b commit 63a27c2
Copy full SHA for 63a27c2

File tree

Expand file treeCollapse file tree

1 file changed

+115
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+115
-1
lines changed
Open diff view settings
Collapse file

‎tutorial.ipynb‎

Copy file name to clipboardExpand all lines: tutorial.ipynb
+115-1Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"#### 09) [Enumerate](#ch9)\n",
3232
"#### 10) [Object Inspection](#ch10)\n",
3333
"#### 11) [Comprehensions](#ch11)\n",
34-
"#### 12) [Exceptions](#ch12)"
34+
"#### 12) [Exceptions](#ch12)\n",
35+
"#### 13) [OOP(Object Oriented Python)](#ch13)"
3536
]
3637
},
3738
{
@@ -3021,6 +3022,119 @@
30213022
"That is all for today!"
30223023
]
30233024
},
3025+
{
3026+
"cell_type": "code",
3027+
"execution_count": null,
3028+
"metadata": {},
3029+
"outputs": [],
3030+
"source": []
3031+
},
3032+
{
3033+
"cell_type": "markdown",
3034+
"metadata": {},
3035+
"source": [
3036+
"<a id=\"ch13\"></a>\n",
3037+
"## Chapter 13 - OOP (Object Oriented Python)"
3038+
]
3039+
},
3040+
{
3041+
"cell_type": "markdown",
3042+
"metadata": {},
3043+
"source": [
3044+
"The concepts of Class and Objects in Python (or any other object oriented language) is one of the most essential features for solving problems with relevant ease and reuse. \n",
3045+
"This tutorial doesn't cover the fundamentals of class and objects but the inner mechanisms and some of the confusing cases with object oriented python."
3046+
]
3047+
},
3048+
{
3049+
"cell_type": "markdown",
3050+
"metadata": {},
3051+
"source": [
3052+
"#### Object inheritance\n",
3053+
"There is a subtle difference with the old and the new style classes in Python -- inheriting objects"
3054+
]
3055+
},
3056+
{
3057+
"cell_type": "code",
3058+
"execution_count": 3,
3059+
"metadata": {},
3060+
"outputs": [
3061+
{
3062+
"name": "stdout",
3063+
"output_type": "stream",
3064+
"text": [
3065+
"I am an old class\n",
3066+
"I am a jazzy new class\n"
3067+
]
3068+
}
3069+
],
3070+
"source": [
3071+
"class OldClass():\n",
3072+
" def __init__(self):\n",
3073+
" print('I am an old class')\n",
3074+
"\n",
3075+
"class NewClass(object):\n",
3076+
" def __init__(self):\n",
3077+
" print('I am a jazzy new class')\n",
3078+
"\n",
3079+
"old = OldClass()\n",
3080+
"# Output: I am an old class\n",
3081+
"\n",
3082+
"new = NewClass()\n",
3083+
"# Output: I am a jazzy new class"
3084+
]
3085+
},
3086+
{
3087+
"cell_type": "markdown",
3088+
"metadata": {},
3089+
"source": [
3090+
"This inheritance from object allows new style classes to utilize some `magic`. A major advantage is that you can employ some useful optimizations like `__slots__`. You can use `super()` and descriptors and the likes. Bottom line? Always try to use new-style classes."
3091+
]
3092+
},
3093+
{
3094+
"cell_type": "markdown",
3095+
"metadata": {},
3096+
"source": [
3097+
"#### Magic Methods\n",
3098+
"\n",
3099+
"Python’s classes are famous for their magic methods, commonly called dunder (double underscore) methods. Let's discuss a few of them.\n",
3100+
"\n",
3101+
"#### * `__init__`"
3102+
]
3103+
},
3104+
{
3105+
"cell_type": "code",
3106+
"execution_count": 5,
3107+
"metadata": {},
3108+
"outputs": [
3109+
{
3110+
"name": "stdout",
3111+
"output_type": "stream",
3112+
"text": [
3113+
"Greetings!!\n",
3114+
"I am another method which is not automatically called\n"
3115+
]
3116+
}
3117+
],
3118+
"source": [
3119+
"class GetTest(object):\n",
3120+
" def __init__(self):\n",
3121+
" print('Greetings!!')\n",
3122+
" def another_method(self):\n",
3123+
" print('I am another method which is not'\n",
3124+
" ' automatically called')\n",
3125+
"\n",
3126+
"a = GetTest()\n",
3127+
"\n",
3128+
"a.another_method()"
3129+
]
3130+
},
3131+
{
3132+
"cell_type": "markdown",
3133+
"metadata": {},
3134+
"source": [
3135+
"#### * `getitem`"
3136+
]
3137+
},
30243138
{
30253139
"cell_type": "code",
30263140
"execution_count": null,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.