PCAP-31-03 practice question 7 of 25
Is it possible to safely check if a class object has a certain attribute0
Full set: 147 questionsThe built-in function hasattr(obj, name) returns True when the supplied object possesses an attribute whose identifier is given by the second argument, and False otherwise. Because it is part of Python’s built-ins, it can be invoked on any object and constitutes the canonical, safe way to test for the presence of an attribute.
A. Describes “hasattr” as an attribute; it is a function, not an attribute.
B. Calls hasattr a method; it is a top-level built-in function, not bound to any class or object.
D. Python provides hasattr(); therefore the statement that no safe check exists is false.
1. Python Software Foundation, “Built-in Functions,” Python 3.12.3 docs, §4, hasattr() paragraph (https://docs.python.org/3/library/functions.html#hasattr).
2. MIT OpenCourseWare 6.0001, Lecture 8 Notes, slide “Testing Attributes with hasattr()”.
Keep practicing
Take the free PCAP-31-03 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCAP-31-03 practice questions
- Question 3How many elements will the list2 list contain after execution of the following snippet? l…
- Question 6Select the valid fun () invocations: (select two answers) def fun (a, b=0): return a*b
- Question 10Which of the equations are True? (Select two answers)
- Question 13Which of the following lambda function definitions are correct? (Select two answers)
- Question 15Which of the following invocations are valid? (Select two answers)
- Question 16Which of the following words can be used as a variable name? (Select two valid names)
