{"id":8283,"date":"2023-11-09T18:43:37","date_gmt":"2023-11-09T13:13:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=8283"},"modified":"2023-11-10T12:23:49","modified_gmt":"2023-11-10T06:53:49","slug":"python-data-persistence-property-dgcorator","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-property-dgcorator\/","title":{"rendered":"Python Data Persistence – @Property DGCOrator"},"content":{"rendered":"
Although a detailed discussion on decorators is beyond the scope of this book, a brief introduction is necessary before we proceed to use @property decorator.<\/p>\n
The function is often termed a callable object. A function is also a passable object. Just as we pass a built-in object viz. number, string, list, and so on. as an argument to a function, we can define a function that receives another function as an argument. Moreover, you can have a function defined in another function (nested function), and a function whose return value is a function itself. Because of all these features, a function is called a first-order object.<\/p>\n
The decorator function receives a function argument. The behavior of the argument function is then extended by wrapping it in a nested function. Definition of function subjected to decoration is preceded by name of decorator prefixed with @ symbol.
\nPython-OOP – 113<\/p>\n
Example<\/strong><\/p>\n We shall now use the property ( ) function as a decorator and define a name () method acting as a getter method for my name attribute in my class. py code above.<\/p>\n Example<\/strong><\/p>\n A property object\u2019s getter, setter, and deleter methods are also decorators. Overloaded name ( ) and age ( ) methods are decorated with name, setter, and age. setter decorators respectively.<\/p>\n Example<\/strong><\/p>\n When @property decorator is used, separate getter and setter methods defined previously are no longer needed. The complete code of myclass.py is as below:<\/p>\n Example<\/strong><\/p>\n Just import above class and test the functionality of property objects using decorators.<\/p>\n Example<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python Data Persistence – @Property DGCOrator Although a detailed discussion on decorators is beyond the scope of this book, a brief introduction is necessary before we proceed to use @property decorator. The function is often termed a callable object. A function is also a passable object. Just as we pass a built-in object viz. number, …<\/p>\ndef adecorator(function): \r\ndef wrapper():\r\nfunction() \r\nreturn wrapper \r\n@adecorator \r\ndef decorate( ): \r\npass<\/pre>\n
@property \r\ndef name(self):\r\nreturn self. ___myname\r\n@property \r\ndef age(self):\r\nreturn self. __myage<\/pre>\n
@name.setter\r\ndef name(self,name):\r\nself. ___myname=name\r\n@age.setter\r\ndef age(self, age):\r\nself. myage=age<\/pre>\n
#myclass. py\r\nclass MyClass:\r\n__slots__=['__myname', '__myage']\r\ndef__init__(self, name=None, age=None):\r\nself.__myname=name\r\nself.__myage=age\r\n\r\n@property\r\ndef name(self) :\r\nprint ('name getter method')\r\nreturn self.__myname\r\n\r\n@property\r\ndef age(self) :\r\nprint ('age getter method')\r\nreturn self. ___myage\r\n\r\n@name.setter\r\ndef name(self,name):\r\nprint ('name setter method')\r\nself.___myname=name\r\n\r\n@age.setter\r\ndef age(self, age):\r\nprint ('age setter method')\r\nself.__myage=age\r\n\r\ndef about(self):\r\nprint ('My name is { } and I am { } years old'.format(self. myname,self. myage))<\/pre>\n
>>> from myclass import MyClass\r\n>>> obj1=MyClass('Ashok', 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()\r\nMy name is Ashok and I am 30 years old<\/pre>\n