{"id":8263,"date":"2023-11-09T18:43:15","date_gmt":"2023-11-09T13:13:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=8263"},"modified":"2023-11-10T12:24:18","modified_gmt":"2023-11-10T06:54:18","slug":"python-data-persistence-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/","title":{"rendered":"Python Data Persistence object-oriented programming"},"content":{"rendered":"

Python Data Persistence – OOP<\/h2>\n

The object-oriented programming paradigm has emerged as a cornerstone of modern software development. Python too is a predominantly object-oriented language, although it supports classical procedural approach also. Python\u2019s implementation of OOP is a little different from that of C++ and Java, and is in keeping with \u2018Simple is better than Complex\u2019 – one of the design principles of Python. Let us not go into much of the theory of object-oriented programming methodology and its advantages. Instead, let us dive straight into Python\u2019s brand of OOP!<\/p>\n

Everything in Python is an object. This statement has appeared more than once in previous chapters. What is an object though? Simply put, object is a piece of data that has a tangible representation in computer\u2019s memory. Each object is characterized by attributes and behaviour as stipulated in a template definition called class. In Python, each instance of any data type is an object representing a certain class. Python\u2019s built-in type ( ) function tells you to which class an object belongs to. (figure 4.1)<\/p>\n

>>> num=1234\r\n>>> type(num)\r\n<class 'int'>\r\n>>> complexnum=2+3j\r\n>>> type(complexnum)\r\n<class 'complex'>\r\n>>> prices={ 'pen' :50, 'book' :200}\r\n>>> type(prices)\r\n<class 'diet'>\r\n>>><\/pre>\n

Each Python object also possesses an attribute__class__that returns class. (figure 4.2)<\/p>\n

>>> num.__class__\r\n<class 'int'>\r\n>>> complexnum.__class__\r\n<class 'complex'>\r\n>>> prices.__class__\r\n<class 'diet'>\r\n>>><\/pre>\n

Hence, it is clear that num is an object of int class, a complexion is an object of the complex class, and so on. As mentioned above, an object possesses attributes (also called data descriptors) and methods as defined in its respective class. For example, the complex class defines real and image attributes that return the real and imaginary parts of a complex number object. It also has conjugate () method returning a complex number with the imaginary part of the opposite sign, (figure 4.3)<\/p>\n

>>> complexnum=2+3j\r\n>>> #attr.ibutes\r\n. . .\r\n>>> complexnum.real\r\n2.0\r\n>>> complexnum.imag\r\n3.0\r\n>>> #method\r\n. . .\r\n>>> complexnum.conjugate()\r\n(2-3j )<\/pre>\n

In the above example, real and imag are the instance attributes as their values will be different for each object of the complex class. Also, the conjugate 0 method is an instance method. A class can also have class-level attributes and methods which are shared by all objects. We shall soon come across their examples in this chapter.<\/p>\n

Built-in data types in Python represent classes defined in the builtins module. This module gets loaded automatically every time when the interpreter starts. The class hierarchy starts with object class. The builtins module also defines Exception and built-in functions (many of which we have come across in previous chapters).<\/p>\n

The following diagram rightly suggests that built-in classes (int, float, complex, bool, list, tuple, and diet) are inherited from the object class. Inheritance incidentally is one of the characteristic features of Object-Oriented Programming Methodology. The base attribute of each of these classes will confirm this.<\/p>\n

Python\u2019s built-in function library also has issubclass ( ) function to test if a certain class is inherited from another. We can use this function and confirm that all built-in classes are sub-classes of the object classes. Interestingly, class is also an object in Python. Another built-in function is\u00b1nstance() returns True if the first argument is an object of the second argument which has to be a class. By the way, type ( ) of any built-in class returns type which happens to be a metaclass of which all classes are objects, is itself a subclass of the object as its base class, (figure 4.4)<\/p>\n

\"Python<\/p>\n

Following interpreter, activity shows that bool class is derived from int class, which is a subclass of object class and also an object of object class!<\/p>\n

Example<\/strong><\/p>\n

>>> isinstance(int, object)\r\nTrue\r\n>>> issubclass(bool, int)\r\nTrue\r\n>>> int. bases\r\n(<class 'object'>,)\r\n>>> isinstance(int, object)\r\nTrue<\/pre>\n

Each class also has access to another attribute__mro__(method resolution order) which can show the class hierarchy of inheritance. The bool class is inherited from int and int from the object.<\/p>\n

Example<\/strong><\/p>\n

>>> bool.__mro__\r\n(<class 'boo'>, <class 'int'>, <class 'object'>)<\/pre>\n

As mentioned above, type () on any built-in class returns type class which in turn is both a subclass and instance of the object class.<\/p>\n

Example<\/strong><\/p>\n

>>> type(bool)\r\n<class 'type'>\r\n>>> type.__bases__\r\n(<class 'object'>,)\r\n>>> isinstance(type, object)\r\nTrue\r\n>>> issubclass(type, object)\r\nTrue<\/pre>\n

Other important constituents of Python program such as functions or modules are also objects, as the following interpreter activity demonstrates:<\/p>\n

Example<\/strong><\/p>\n

>>> #function from a module\r\n. . .\r\n>>> import math\r\n>>> type(math.sqrt)\r\n<class 'builtin_function_or_method'>\r\n>>> #built-in function\r\n. . .\r\n>>> type(id)\r\ncclass 'builtin_function_or_method'>\r\n>>> #user defined function\r\n. . .\r\n>>> def hello( ):\r\n... print ('Hello World')\r\n. . .\r\n>>> type(hello)\r\n<class 'function'>\r\n>>> #module is also an object\r\n. . .\r\n>>> type(math)\r\n<class 'module'><\/pre>\n

We have used the range ( ) function in connection with for loop. It actually returns a range object which belongs to the range class.<\/p>\n

Example<\/strong><\/p>\n

>>> #range is also an object\r\n. . .\r\n>>> obj=range(10)\r\n>>>type(obj)\r\n<class 'range'>\r\n>>> >>> range._bases__\r\n(<class 'object'>,)<\/pre>\n","protected":false},"excerpt":{"rendered":"

Python Data Persistence – OOP The object-oriented programming paradigm has emerged as a cornerstone of modern software development. Python too is a predominantly object-oriented language, although it supports classical procedural approach also. Python\u2019s implementation of OOP is a little different from that of C++ and Java, and is in keeping with \u2018Simple is better than …<\/p>\n

Python Data Persistence object-oriented programming<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Data Persistence object-oriented programming - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Persistence object-oriented programming - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Persistence – OOP The object-oriented programming paradigm has emerged as a cornerstone of modern software development. Python too is a predominantly object-oriented language, although it supports classical procedural approach also. Python\u2019s implementation of OOP is a little different from that of C++ and Java, and is in keeping with \u2018Simple is better than … Python Data Persistence object-oriented programming Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-09T13:13:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:54:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png\",\"width\":262,\"height\":537,\"caption\":\"Python Data Presistence - OOP chapter 4 img 1\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/\",\"name\":\"Python Data Persistence object-oriented programming - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage\"},\"datePublished\":\"2023-11-09T13:13:15+00:00\",\"dateModified\":\"2023-11-10T06:54:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Persistence object-oriented programming\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Persistence object-oriented programming\",\"datePublished\":\"2023-11-09T13:13:15+00:00\",\"dateModified\":\"2023-11-10T06:54:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage\"},\"wordCount\":659,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Data Persistence object-oriented programming - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Persistence object-oriented programming - Python Programs","og_description":"Python Data Persistence – OOP The object-oriented programming paradigm has emerged as a cornerstone of modern software development. Python too is a predominantly object-oriented language, although it supports classical procedural approach also. Python\u2019s implementation of OOP is a little different from that of C++ and Java, and is in keeping with \u2018Simple is better than … Python Data Persistence object-oriented programming Read More »","og_url":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-09T13:13:15+00:00","article_modified_time":"2023-11-10T06:54:18+00:00","og_image":[{"url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png"}],"twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png","width":262,"height":537,"caption":"Python Data Presistence - OOP chapter 4 img 1"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage","url":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/","name":"Python Data Persistence object-oriented programming - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage"},"datePublished":"2023-11-09T13:13:15+00:00","dateModified":"2023-11-10T06:54:18+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Persistence object-oriented programming"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Persistence object-oriented programming","datePublished":"2023-11-09T13:13:15+00:00","dateModified":"2023-11-10T06:54:18+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#webpage"},"wordCount":659,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"image":{"@id":"https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/06\/Python-Data-Presistence-OOP-chapter-4-img-1.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-data-persistence-object-oriented-programming\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8263"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=8263"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8263\/revisions"}],"predecessor-version":[{"id":11327,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8263\/revisions\/11327"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}