{"id":24809,"date":"2021-11-02T09:48:11","date_gmt":"2021-11-02T04:18:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=24809"},"modified":"2021-11-05T20:37:24","modified_gmt":"2021-11-05T15:07:24","slug":"python-hasattr-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-hasattr-method-with-examples\/","title":{"rendered":"Python hasattr() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python globals() Function with Examples<\/a> If the specified object has the specified attribute, the hasattr() function returns true; otherwise, it returns False.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values<\/strong><\/p>\n object:<\/strong> This is required. It is an object.<\/p>\n attribute:<\/strong> The name of the attribute to be checked whether exists or not.<\/p>\n Return Value:<\/strong><\/p>\n The method hasattr() returns:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python globals() Function with Examples hasattr() Method in Python: If the specified object has the specified attribute, the hasattr() function returns true; otherwise, it returns False. Syntax: hasattr(object, attribute) Parameter Values object: This is required. It is an object. attribute: The name of the attribute to be checked …<\/p>\n
\nhasattr() Method in Python:<\/strong><\/p>\nhasattr(object, attribute)<\/pre>\n
\n
hasattr() Method 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 = 'Hitler'\r\n\r\n\r\n# Create an object for the class and store it in a variable.\r\nEmploydetailsobj = Employdetails()\r\n# Check if the above class has an attribute id by passing arguments like the\r\n# above object and attribute name using the hasattr() function and print the result.\r\nprint('Does Employdetails has id?:', hasattr(Employdetailsobj, 'id'))\r\n# Similarly, do the same for the other attribute(jobrole) and print the result.\r\nprint('Does Employdetails has jobrole?:', hasattr(Employdetailsobj, 'jobrole'))\r\n<\/pre>\n
Does Employdetails has id?: True\r\nDoes Employdetails has jobrole?: False<\/pre>\n
Method #2: Using Built-in Functions (User 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 = 'Hitler'\r\n\r\n\r\n# Create an object for the class and store it in a variable.\r\nEmploydetailsobj = Employdetails()\r\n# Give some random attribute name as user input using the input() function\r\n# and store it in a variable.\r\natrname1 = input(\"Enter some random attribute name = \")\r\n# Give the other attribute name as user input using the input() function\r\n# and store it in another variable.\r\natrname2 = input(\"Enter some random attribute name = \")\r\n# Check if the above class has the above attribute by passing arguments like the above object\r\n# and above attribute name using the hasattr() function and print the result.\r\nprint('Does Employdetails has', atrname1, '?:',\r\n hasattr(Employdetailsobj, atrname1))\r\n# Similarly, do the same for the other attribute and print the result.\r\nprint('Does Employdetails has', atrname2, '?:',\r\n hasattr(Employdetailsobj, atrname2))\r\n<\/pre>\n
Enter some random attribute name = age\r\nEnter some random attribute name = ename\r\nDoes Employdetails has age ?: False\r\nDoes Employdetails has ename ?: True<\/pre>\n
\n