PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 31


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

Show Answer
Correct Answer: B

It is possible to check if a class or object has a certain attribute by using the hasattr() function. The hasattr() function takes two arguments: the object or class to check, and the name of the attribute as a string. It returns True if the attribute exists and False otherwise. Therefore, the correct answer is 'yes, by using the hasattr() method'.

Discussion

17 comments
Sign in to comment
anjuvinayanOption: B
Nov 29, 2019

hasattr ( ) is a function and is not a method.answer is still B since spelling of hasattr is C is wrong

Adeshina
Dec 12, 2022

You are correct that the hasattr() function is not a method. In Python, methods are functions that are defined inside a class and are associated with objects of that class, while functions are standalone blocks of code that are not associated with any particular class or object. However, the hasattr() function can still be used to safely check for the presence of an attribute on an object, even though it is not a method. To use the hasattr() function, you simply pass the object as the first argument and the name of the attribute as the second argument

echarlotteefOption: C
Jan 15, 2020

Correct answer is C, 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)

saturn_samOption: B
Sep 8, 2023

check spelling of option C

CoinUmbrellaOption: B
May 18, 2024

Option B: Correct because hasattr() is indeed a method (function) provided by Python to check if an object has a certain attribute. Option C: Incorrect because it has a typo. The correct function is hasattr(), not hassattr().

JnanadaOption: C
Aug 18, 2022

Answer should be C with hasattr function

andr3Option: C
Feb 27, 2023

its a function https://docs.python.org/3/library/functions.html#hasattr

nitish1418Option: B
Apr 13, 2023

read carefully, one is hasattr() and other is hassattr() (extra s in has). People are debating over function vs method. So correct answer is B

mamashOption: B
May 18, 2023

Answer is B just read https://www.w3schools.com/python/ref_func_hasattr.asp

Valcon_doo_NoviSad
Oct 12, 2023

Dude, the title of this article literally says it's a function.

Ello2023Option: A
May 22, 2023

The answer is A. It does not ask what hasattr is but if you can safely check for an attribute in a class. So if you use hasattr function and the attribute you can find it.

Philipus18192021Option: A
May 30, 2023

A, is the correct answer because c will give a syntax error .it is hasattr and not hassattr.

Ello2023Option: B
Jun 14, 2023

B is correct as the spellings and it being a method/function is correct.

Iphy2xyOption: C
Jun 21, 2023

Correct answer is C: hasattr() is a function not a method. the extra 's' on option C could be a typo error. =====> Using hasattr() function: The hasattr() function can be used to determine if an object or class has a specific attribute. It takes two arguments: the object or class to check, and the name of the attribute as a string. It returns True if the attribute exists and False otherwise

CaptainPirateOption: B
Aug 28, 2023

STOP: You guys dont tell me you cant notice the spelling of hasattr() and hassattr().

CaptainPirate
Aug 28, 2023

Thats the key to the answer.

Valcon_doo_NoviSadOption: C
Oct 12, 2023

Hasattr is a Python function, which can be easily checked in an interpreter.

seaverickOption: B
Jan 23, 2024

#question 31 class Person: age = 23 name = "Adam" person = Person() print("Person's age:", hasattr(person, "age")) print("Person's salary:", hasattr(person, "salary")) # Output: # Person's age: True # Person's salary: False Tested: B

seaverickOption: C
Feb 3, 2024

https://docs.python.org/3/library/functions.html hasattr() is a function

G3n
Jun 18, 2024

The answers in the dumps are good to remember! The bulk of the exam questions are from these exam dumps. The questions with mistakes in them or missing : etc are in the exam but in the correct answer form, do remember the correct answers as well as the answers with the mistakes in the coding. There are a few questions that are slightly different but you can find the answers from the previous or next question/answers! Hope this helps!