{"id":25537,"date":"2021-11-19T09:12:44","date_gmt":"2021-11-19T03:42:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=25537"},"modified":"2021-11-19T09:12:44","modified_gmt":"2021-11-19T03:42:44","slug":"python-delattr-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-delattr-function-with-examples\/","title":{"rendered":"Python delattr() Function with Examples"},"content":{"rendered":"
delattr() Function in Python:<\/strong><\/p>\n The delattr() function removes the specified attribute from the given object.<\/p>\n In Python, there is another operator that performs the same function as the delattr() method and that is the<\/p>\n del operator.<\/p>\n del operator versus delattr () function:<\/strong><\/p>\n Dynamic deletion:<\/strong> The del function is more explicit and efficient, and the delattr() function allows for dynamic attribute deletion.<\/p>\n Speed:<\/strong> When the programs are considered and executed, there is a slight difference in execution speed. Depending on the machine, del is slightly faster than delattr().<\/p>\n byte-code Instructions:<\/strong> del uses fewer byte-code instructions than delattr ().<\/p>\n So we conclude the comparison by saying that del is slightly faster than delattr, but delattr() has an advantage when it comes to dynamic deletion of attributes, which the del operator does not support.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> This is Required. It is an object.<\/p>\n attribute: <\/strong>This is Required. The name of the attribute to be removed<\/p>\n Return Value:<\/strong><\/p>\n delattr() does not return any outcome (returns None). It only deletes one attribute (if the object allows it).<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" delattr() Function in Python: The delattr() function removes the specified attribute from the given object. In Python, there is another operator that performs the same function as the delattr() method and that is the del operator. del operator versus delattr () function: Dynamic deletion: The del function is more explicit and efficient, and the delattr() …<\/p>\ndelattr(object, attribute)<\/pre>\n
delattr() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Create a class say Employdetails.\r\nclass Employdetails:\r\n # Take a variable and initialize it with some random number(id).\r\n id = 10\r\n # Take another variable and initialize it with some random name(ename).\r\n ename = 'virat'\r\n # Take another variable and initialize it with some random jobrole(jobrole).\r\n jobrole = 'developer'\r\n\r\n\r\n# Create an object for the given class and store it in a variable.\r\nclassobj = Employdetails()\r\n# Print the value of the attribute by using the above declared object.\r\nprint(\"The attribute is :\",classobj.ename)\r\n# Pass the above class name and attribute name to the delattr() function to delete\r\n# given attribute from the class.\r\ndelattr(Employdetails, 'ename')\r\n# Print the value of the attribute by using the above declared object after deleting.\r\nprint(\"The given attribute after deleting from class = \",classobj.ename)\r\n<\/pre>\n
The attribute is : virat\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 19, in <module>\r\nprint(\"The given attribute after deleting from class = \",classobj.ename)\r\nAttributeError: 'Employdetails' object has no attribute 'ename'<\/pre>\n
Here first it prints the given attribute 'ename'.\r\nThen delete that attribute from the class 'Employdetails'.\r\nAgain try to print the given attribute 'ename'.\r\nNow, it raises an error since the attribute 'ename' got deleted from it.<\/pre>\n