nerdexam
Python_Institute

PCAP-31-03 · Question #110

Is it possible to safely check if a class/object has a certain attribute?

The correct answer is C. yes, by using the hassattr ( ) function. Information from Python course, singned by Python Institute: Python provides a function which is able to safely check if any object/class contains a specified property. The function is named hasattr, and expects two arguments to be passed to it: the class or the object being chec

Technology and architecture

Question

Is it possible to safely check if a class/object has a certain attribute?

Options

  • Ayes, by using the hasattr attribute
  • Byes, by using the hasattr ( ) method
  • Cyes, by using the hassattr ( ) function
  • Dno, it is not possible

How the community answered

(46 responses)
  • A
    11% (5)
  • B
    4% (2)
  • C
    80% (37)
  • D
    4% (2)

Explanation

Information from Python course, singned by Python Institute: Python provides a function which is able to safely check if any object/class contains a specified property. The function is named hasattr, and expects two arguments to be passed to it: the class or the object being checked; the name of the property whose existence has to be reported (note: it has to be a string containing the attribute name, not the name alone)

Topics

#Python OOP#hasattr function#attribute checking#introspection

Community Discussion

No community discussion yet for this question.

Full PCAP-31-03 Practice