{"id":8281,"date":"2023-11-09T18:43:35","date_gmt":"2023-11-09T13:13:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=8281"},"modified":"2023-11-10T12:23:57","modified_gmt":"2023-11-10T06:53:57","slug":"python-data-persistence-property-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-property-function\/","title":{"rendered":"Python Data Persistence – property () Function"},"content":{"rendered":"

Python Data Persistence – property () Function<\/h2>\n

Well, let me now make a volte-face here. It\u2019s not that Python doesn\u2019t have private\/protected access restrictions. It does have. If an instance variable is prefixed with double underscore character \u2018___\u2019, it behaves like a private variable. Let us see how:<\/p>\n

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

>>> #private variable in class\r\n. . .\r\n>>> class tester:\r\n. . .\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0def___init___(self) :\r\n. . .\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0self.__var=10\r\n. . .\r\n>>><\/pre>\n

Try .declaring an object of above class and access its__var attribute.<\/p>\n

>>> t=tester( )\r\n>>> t.__var\r\nTraceback (most recent call last):\r\nFile \"<stdin>\", line 1, in <module>\r\nAttributeError: 'tester' object has no attribute '___var'<\/pre>\n

The AttributeError indicates that the variable prefixed is inaccessible. However, it can still be accessed from outside the class. A double underscore prefixed attribute is internally renamed as obj ._class var. This is called name mangling mechanism.<\/p>\n

>>> t._tester var\r\n10<\/pre>\n

Hence, the so-called private variable in Python is not really private. However, a property object acts as an interface to the getter and setter methods of a private attribute of the Python class.
\nPython\u2019s built-in property() function uses getter, setter, and delete functions as arguments and returns a property object. You can retrieve as well as assign to the property as if you do with any variable. When some value is assigned to a property object, its setter method is called. Similarly, when a property object is accessed, its getter method is called.<\/p>\n

propobj=property(fget, fset, fdel, doc)<\/pre>\n

In the above function signature, fget is the getter method in the class, fset is the setter method and, fdel is the delete method. The doc parameter sets the docstring of the property object.
\nIn our ongoing myclass.py script, let us now define age and name properties to interface with ___myage and ___myname private instance attributes.<\/p>\n

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

#myclass.py\r\nclass MyClass:\r\n        __slots___= ['__myname' ,'___myage ' ]\r\n        def__init__(self, name=None, age=None):\r\n               self. myname =name\r\n               self. myage=age\r\n        def getname(self):\r\n            print ('name getter method')\r\n            return self. ___myname\r\n       def setname(self, name):\r\n           print ('name setter method')\r\n           self. ___myname=name\r\n      def getage(self):\r\n           print ('age getter method')\r\n           return self.____myage\r\n      def setage(self, age):\r\n           print ('age setter method') \r\n       self. ____myage=age\r\nname=property(getname, setname, \"Name\") age=property(getage, setage, \"Age\") def about(self) :\r\nprint ('My name is { } and I am { } years old'.format(self.___myname,self. __myage))<\/pre>\n

We\u2019ll now import this class and use the properties. Any attempt to retrieve or change the value of property calls its corresponding getter or setter and changes the internal private variable. Note that the getter and setter methods have a print () message inserted to confirm this behavior. The about () method will also show changes in private variables due to manipulations of property objects.<\/p>\n

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

>>> from myclass import MyClass\r\n>>> obj1=MyClass(1Ashok' , 21)\r\n>>> obj1.about() #initial values of object's attributes\r\nMy name is Ashok and I am 21 years old\r\n>>> #change age property\r\n>>> obj1.age=30\r\nage setter method\r\n>>> #access name property\r\n>>> obj1.name\r\nname getter method\r\n'Ashok'\r\n>>> obj1.about( ) #object's attributes after property changes\r\nMy name is Ashok and I am 30 years old<\/pre>\n

So, finally, we get the Python class to work almost similar to how the OOP methodology defines. There is a more elegant way to define property objects – by using @property decorator.<\/p>\n","protected":false},"excerpt":{"rendered":"

Python Data Persistence – property () Function Well, let me now make a volte-face here. It\u2019s not that Python doesn\u2019t have private\/protected access restrictions. It does have. If an instance variable is prefixed with double underscore character \u2018___\u2019, it behaves like a private variable. Let us see how: Example >>> #private variable in class . …<\/p>\n

Python Data Persistence – property () Function<\/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 - property () Function - 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-property-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Persistence - property () Function - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Persistence – property () Function Well, let me now make a volte-face here. It\u2019s not that Python doesn\u2019t have private\/protected access restrictions. It does have. If an instance variable is prefixed with double underscore character \u2018___\u2019, it behaves like a private variable. Let us see how: Example >>> #private variable in class . … Python Data Persistence – property () Function Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-persistence-property-function\/\" \/>\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:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:53:57+00:00\" \/>\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=\"3 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\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/\",\"name\":\"Python Data Persistence - property () Function - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-09T13:13:35+00:00\",\"dateModified\":\"2023-11-10T06:53:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-property-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Persistence – property () Function\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Persistence – property () Function\",\"datePublished\":\"2023-11-09T13:13:35+00:00\",\"dateModified\":\"2023-11-10T06:53:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage\"},\"wordCount\":354,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-property-function\/#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 - property () Function - 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-property-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Persistence - property () Function - Python Programs","og_description":"Python Data Persistence – property () Function Well, let me now make a volte-face here. It\u2019s not that Python doesn\u2019t have private\/protected access restrictions. It does have. If an instance variable is prefixed with double underscore character \u2018___\u2019, it behaves like a private variable. Let us see how: Example >>> #private variable in class . … Python Data Persistence – property () Function Read More »","og_url":"https:\/\/python-programs.com\/python-data-persistence-property-function\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-09T13:13:35+00:00","article_modified_time":"2023-11-10T06:53:57+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"3 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":"WebPage","@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage","url":"https:\/\/python-programs.com\/python-data-persistence-property-function\/","name":"Python Data Persistence - property () Function - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-09T13:13:35+00:00","dateModified":"2023-11-10T06:53:57+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-persistence-property-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Persistence – property () Function"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Persistence – property () Function","datePublished":"2023-11-09T13:13:35+00:00","dateModified":"2023-11-10T06:53:57+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-property-function\/#webpage"},"wordCount":354,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-data-persistence-property-function\/#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\/8281"}],"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=8281"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8281\/revisions"}],"predecessor-version":[{"id":9422,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8281\/revisions\/9422"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}