Certified Associate in Python Programming (PCAP) — Question 5
Is it possible to safely check if a class/object has a certain attribute?
Answer options
- A. yes, by using the hasattr attribute
- B. yes, by using the hasattr ( ) method
- C. yes, by using the hassattr ( ) function
- D. no, it is not possible
Correct answer: B
Explanation
The correct answer is B because the hasattr() method is the proper way to check for an attribute in Python. Option A incorrectly refers to it as an attribute instead of a method, while option C contains a typo in the method name, and option D is incorrect as it is indeed possible to check for attributes.