Guest User

Untitled

a guest
Feb 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## Python Object-Oriented Programming Tutorial Concepts\n",
  8. "Object-Oriented programming (OOP) is a popular programming pardaigm which contains the concept of \"objects\" also that includes both data and called as \"fields\" and functions that is called as \"methods\".There are many Object-Oriented programming language ,Some of them are C++,Java,Python etc.\n",
  9. "\n",
  10. "**Python** has been an Object-Oriented language since it existed. Because of this, creating and using classes and objects are downright easy. Python also supports the paradigm of Object-Oriented Programming.You can create and use the classes and objects easily in python.If you work as a data scientist,knowing some concepts from OOP is useful, of course, but you probably won’t use much of it.But,you can see the beauty of the Object-Oriented pradagim through Python.\n",
  11. "\n",
  12. "In any Object-Oriented programming, there are 4 basic principles that are used. These are:<br/>\n",
  13. "**Encapsulation:** hiding unnecessary details from the user.<br />\n",
  14. "**Inheritance:** reusing code from one class i.e. Properies of parent class, in the child class i.e.very similar to the the code that is needed for another class.<br />\n",
  15. "**Abstraction:** using what we know to work with something without knowing how it completely works (example: you don't know the inner workings of a Television, but you know how to press the buttons and you can see news,movies etc in it ).<br />\n",
  16. "**Polymorphism:** taking a base class method and overwriting it so that the child class has the same method name, but a different implementation specific for that child class.i.e.It can also be taken as \"one name but different forms\".\n",
  17. "\n",
  18. "\n",
  19. "In this tutorial,you will see about basics of Python OOP'S in detail:\n",
  20. "* You will learn about the concept of Classes and Objects.\n",
  21. "* You will learn about Defining Classes and Objects in Python,with all the concept included in a single example.\n",
  22. "* You will learn about the concept of four basic principles of object-oriented programming that any Object-Oriented language \n",
  23. "\n",
  24. "Also,be sure to follow the series of tutorials related to Object-Oriented Programming concepts in coming days to get more indepth knowledge in these topics.\n"
  25. ]
  26. },
  27. {
  28. "cell_type": "markdown",
  29. "metadata": {},
  30. "source": [
  31. "## Classes and Objects."
  32. ]
  33. },
  34. {
  35. "cell_type": "markdown",
  36. "metadata": {},
  37. "source": [
  38. "#### Classes\n",
  39. "A class is a blueprint or prototype from which objects are created.\n",
  40. "\n",
  41. "#### Objects\n",
  42. "Objects are a representation of the real world objects like house, dogs, bike, etc. which are bundle of related state and behaviors.The objects share two main characteristics: data called as \"fields\" and behavior and called as \"methods\".<br />\n",
  43. "\n",
  44. "**\n",
  45. "A class is a blueprint, a model for its objects.For Example you can think it as a blue print for a house i.e. to create a house a blue print is needed i.e. think of it as a model and from which individual objects are created.In the real world you often find many objects with all the same type.All the same make and design(have an same area,color,same number of rooms etc).Each house was built from the same set of blueprints and has the same components.\n",
  46. "**\n",
  47. " "
  48. ]
  49. },
  50. {
  51. "cell_type": "markdown",
  52. "metadata": {},
  53. "source": [
  54. "### Defining a Class in Python\n",
  55. "\n",
  56. "To define a Class, in typical simple Python fashion, you can use the word ‘class,’ followed by the name of your new class.you can make a new class here, called ‘vehicles’. We use a colon after the name, and then anything contained within the class definition is indented. However, with a class, there are no parentheses:\n",
  57. "```\n",
  58. "class NameofClass:\n",
  59. "\n",
  60. " ‘Class purpose/documentation string’\n",
  61. "\n",
  62. " Statements to be executed, data attributes and functions\n",
  63. " ```\n",
  64. "```\n",
  65. "class Car:\n",
  66. "```"
  67. ]
  68. },
  69. {
  70. "cell_type": "markdown",
  71. "metadata": {},
  72. "source": [
  73. "### Creating an Object in Python"
  74. ]
  75. },
  76. {
  77. "cell_type": "markdown",
  78. "metadata": {},
  79. "source": [
  80. "Once you have created the class, you actually need to create an object for each instance of that class. In Python you can create a new variable to create an instance of a class. "
  81. ]
  82. },
  83. {
  84. "cell_type": "markdown",
  85. "metadata": {},
  86. "source": [
  87. "```\n",
  88. "car1 = Car()\n",
  89. "car2 = Car()\n",
  90. "```"
  91. ]
  92. },
  93. {
  94. "cell_type": "markdown",
  95. "metadata": {},
  96. "source": [
  97. "Here is the full example showing the use of Classes and Object concept together."
  98. ]
  99. },
  100. {
  101. "cell_type": "code",
  102. "execution_count": 6,
  103. "metadata": {},
  104. "outputs": [
  105. {
  106. "name": "stdout",
  107. "output_type": "stream",
  108. "text": [
  109. "brum brum\n",
  110. "brum brum\n"
  111. ]
  112. }
  113. ],
  114. "source": [
  115. "class Car(object):\n",
  116. " def __init__(self,brand, model, color):\n",
  117. " # Sets all the properties\n",
  118. " self.brand = brand\n",
  119. " self.model = model\n",
  120. " self.color = color\n",
  121. " \n",
  122. " def start_car(self):\n",
  123. " \n",
  124. " print(\"brum brum\")\n",
  125. "\n",
  126. " \n",
  127. "\n",
  128. "if __name__ == \"__main__\":\n",
  129. " # Creates two instances of new_car, each with unique properties\n",
  130. " carFirst = Car(\"Tesla\",\"Model 3\",\"Grey\")\n",
  131. " carSecond = Car(\"Tata\",\"Tata Nano\",\"Yellow\")\n",
  132. " \n",
  133. "\n",
  134. " carFirst.start_car();\n",
  135. " carSecond.start_car();"
  136. ]
  137. },
  138. {
  139. "cell_type": "markdown",
  140. "metadata": {},
  141. "source": [
  142. "You'll get the following output:\n",
  143. "```\n",
  144. "brum brum\n",
  145. "brum brum\n",
  146. "```"
  147. ]
  148. },
  149. {
  150. "cell_type": "markdown",
  151. "metadata": {},
  152. "source": [
  153. "In Python the names of special methods begin and end with double underscore - __. For example, the special method __init__ is used to initialize the state of newly created objects.For instance, we could create a new car object and set its brand, model, and year attributes on a single line, rather than expending an additional line for each attribute:"
  154. ]
  155. },
  156. {
  157. "cell_type": "markdown",
  158. "metadata": {
  159. "collapsed": true
  160. },
  161. "source": [
  162. "The four principle of the any Object-Oriented programming language are as follows and only terminology is mentioned:"
  163. ]
  164. },
  165. {
  166. "cell_type": "markdown",
  167. "metadata": {},
  168. "source": [
  169. "## Encapsulation\n",
  170. "\n",
  171. "**Encapsulation** is the mechanism by which the internal representation of an object can't be seen from outside of the objects definition.Access to this data is typically only achieved through special methods: Getters and Setters.\n",
  172. "Encapsulation gives you more control over the degree of coupling in your code, it allows a class to change its implementation without affecting other parts of the code.\n",
  173. "There is kind of debate on the topic i.e.Python have encapsulation or not.Python does not have the private keyword, unlike some other Object-Oriented languages, but encapsulation can be done.Instead, it relies on the convention: a class variable that should not directly be accessed should be prefixed with an underscore."
  174. ]
  175. },
  176. {
  177. "cell_type": "markdown",
  178. "metadata": {},
  179. "source": [
  180. "## Inheritance"
  181. ]
  182. },
  183. {
  184. "cell_type": "markdown",
  185. "metadata": {},
  186. "source": [
  187. "**Inheritance** is a way to form new classes using classes that have already been defined. The newly formed classes are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors)."
  188. ]
  189. },
  190. {
  191. "cell_type": "markdown",
  192. "metadata": {},
  193. "source": [
  194. "## Abstraction\n",
  195. "**Abstraction** allows us to bundle together some related things and give a name to the bundle. Once that bundle is named, we can\n",
  196. "refer to it and, at times, ignore the details within the bundle. This is the onlyway to solve complex problems."
  197. ]
  198. },
  199. {
  200. "cell_type": "markdown",
  201. "metadata": {},
  202. "source": [
  203. "## Polymorphism\n"
  204. ]
  205. },
  206. {
  207. "cell_type": "markdown",
  208. "metadata": {},
  209. "source": [
  210. "**Polymorphism**is the ability to appear in the many forms also can be called as \"one name,different forms\".Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes."
  211. ]
  212. },
  213. {
  214. "cell_type": "markdown",
  215. "metadata": {},
  216. "source": [
  217. "## Congrats!"
  218. ]
  219. },
  220. {
  221. "cell_type": "markdown",
  222. "metadata": {},
  223. "source": [
  224. "You have made it to the end of this OOP's tutorial! In this tutorial, you learned about what basics of OOP's concept,tools that are required to make OOP's happen i.e. terminologies like classes,objects etc and also the principal of any Object-Oriented Programming language."
  225. ]
  226. }
  227. ],
  228. "metadata": {
  229. "kernelspec": {
  230. "display_name": "Python 3",
  231. "language": "python",
  232. "name": "python3"
  233. },
  234. "language_info": {
  235. "codemirror_mode": {
  236. "name": "ipython",
  237. "version": 3
  238. },
  239. "file_extension": ".py",
  240. "mimetype": "text/x-python",
  241. "name": "python",
  242. "nbconvert_exporter": "python",
  243. "pygments_lexer": "ipython3",
  244. "version": "3.6.1"
  245. }
  246. },
  247. "nbformat": 4,
  248. "nbformat_minor": 1
  249. }
Add Comment
Please, Sign In to add comment