{"id":8273,"date":"2023-11-09T18:43:28","date_gmt":"2023-11-09T13:13:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=8273"},"modified":"2023-11-10T12:24:11","modified_gmt":"2023-11-10T06:54:11","slug":"python-data-persistence-constructor","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-constructor\/","title":{"rendered":"Python Data Persistence – Constructor"},"content":{"rendered":"
We provide a constructor method to our class to initialize its object. The__init__( ) method defined inside the class works as a constructor. It makes a reference to the object as the first positional argument in the form of a special variable called \u2018self\u2019. Java\/C++ programmers will immediately relate this to \u2018this\u2019 variable. A method with \u2018self argument is known as an instance method. Save the following script as myclass.py. It provides two instance variables (my name and my age) and about () method.<\/p>\n
Example<\/strong><\/p>\n Let us use this script as a module, import MyClass from it and have its object.<\/p>\n Example<\/strong><\/p>\n So, now each object will have the same attributes and methods as defined in the class. However, attributes of each object will be initialized with the same values as assigned in__init__( ) method. To counter this, we can have a parameterized constructor, optionally with default values. Modify Now we can have objects having attributes with different values.<\/p>\n Example<\/strong><\/p>\n However, does this process prevent the dynamic addition of an attribute to an object\/class? Well, not really.<\/p>\n So, how do we ensure that the object will have only those attributes as per the class definition? The answer is slots .<\/p>\n _slots_<\/strong><\/p>\n To prevent any further attribute to be added to an object of a class, it carries a variable named as__slots__<\/strong>. This variable is defined before__init__ () method. It is a list of allowed attributes to be initialized by the constructor. The setattr () function will first check if the second argument is in the__slots__<\/strong>list and assign it a new value only if it exists.<\/p>\n Example<\/strong><\/p>\n Import the modified class and check the effect of__slots___variable.<\/p>\n Example<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python Data Persistence – Constructor We provide a constructor method to our class to initialize its object. The__init__( ) method defined inside the class works as a constructor. It makes a reference to the object as the first positional argument in the form of a special variable called \u2018self\u2019. Java\/C++ programmers will immediately relate this …<\/p>\n#myclass.py \u2018\r\nclass MyClass:\r\n def __init__(self):\r\nself.myname='Ashok'\r\nself.myage=21\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( )\r\n> > > obj1.myname\r\n'Ashok'\r\n>>> obj1.myage\r\n21\r\n>>> obj1.about()\r\nMy name is Ashok and I am 21 years old<\/pre>\n
>>> from myclass import MyClass\r\n>>> obj1=MyClass('Ashok', 21)\r\n>>> obj2=MyClass('Asmita',20)\r\n>>> obj1.about()\r\nMy name is Ashok and I am 21 years old\r\n>>> obj2.about()\r\nMy name is Asmita and I am 20 years old<\/pre>\n
>>> setattr(obj1marks50)\r\n>>> obj1.marks\r\n50\r\n\r\n<\/pre>\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\ndef about(self):\r\n print ('My name is { } and I am { } years old1.format(self.myname,self.myage))<\/pre>\n
>>> from myclass import MyClass\r\n>>> obj1=MyClass('Ashok', 21)\r\n> > > obj1.about( )\r\nMy name is Ashok and I am 21 years old\r\n>>> setattr(obj1, 'marks', 50) #new attribute not allowed\r\nTraceback (most recent call last):\r\nFile \"<stdin>\", line 1, in <module>\r\nAttributeError: 'MyClass' object has no attribute 'marks'\r\n>>> setattr(obj1, 'myage', 25) #value of existing module can be modified\r\n>>> obj1.about()\r\nMy name is Ashok and I am 25 years old<\/pre>\n