|
31 | 31 | "#### 09) [Enumerate](#ch9)\n", |
32 | 32 | "#### 10) [Object Inspection](#ch10)\n", |
33 | 33 | "#### 11) [Comprehensions](#ch11)\n", |
34 | | - "#### 12) [Exceptions](#ch12)" |
| 34 | + "#### 12) [Exceptions](#ch12)\n", |
| 35 | + "#### 13) [OOP(Object Oriented Python)](#ch13)" |
35 | 36 | ] |
36 | 37 | }, |
37 | 38 | { |
|
3021 | 3022 | "That is all for today!" |
3022 | 3023 | ] |
3023 | 3024 | }, |
| 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 | + }, |
3024 | 3138 | { |
3025 | 3139 | "cell_type": "code", |
3026 | 3140 | "execution_count": null, |
|
0 commit comments