Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 31 discussion

Actual exam question from Python Institute's PCAP
Question #: 31
Topic #: 1
[All PCAP Questions]

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

  • 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
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️
Reference:
https://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
anjuvinayan
Highly Voted 4 years, 4 months ago
hasattr ( ) is a function and is not a method.answer is still B since spelling of hasattr is C is wrong
upvoted 17 times
Adeshina
1 year, 4 months ago
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
upvoted 2 times
...
...
echarlotteef
Highly Voted 4 years, 3 months ago
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)
upvoted 8 times
...
seaverick
Most Recent 2 months, 3 weeks ago
Selected Answer: C
https://docs.python.org/3/library/functions.html hasattr() is a function
upvoted 1 times
...
seaverick
3 months ago
#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
upvoted 1 times
...
Valcon_doo_NoviSad
6 months, 2 weeks ago
Selected Answer: C
Hasattr is a Python function, which can be easily checked in an interpreter.
upvoted 1 times
...
saturn_sam
7 months, 2 weeks ago
Selected Answer: B
check spelling of option C
upvoted 2 times
...
CaptainPirate
8 months ago
STOP: You guys dont tell me you cant notice the spelling of hasattr() and hassattr().
upvoted 1 times
CaptainPirate
8 months ago
Thats the key to the answer.
upvoted 1 times
...
...
Iphy2xy
10 months, 1 week ago
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
upvoted 1 times
...
Ello2023
10 months, 2 weeks ago
Selected Answer: B
B is correct as the spellings and it being a method/function is correct.
upvoted 1 times
...
Philipus18192021
11 months ago
A, is the correct answer because c will give a syntax error .it is hasattr and not hassattr.
upvoted 1 times
...
Ello2023
11 months, 1 week ago
Selected Answer: A
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.
upvoted 1 times
...
mamash
11 months, 1 week ago
Selected Answer: B
Answer is B just read https://www.w3schools.com/python/ref_func_hasattr.asp
upvoted 1 times
Valcon_doo_NoviSad
6 months, 2 weeks ago
Dude, the title of this article literally says it's a function.
upvoted 1 times
...
...
nitish1418
1 year ago
Selected Answer: B
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
upvoted 1 times
...
andr3
1 year, 1 month ago
Selected Answer: C
its a function https://docs.python.org/3/library/functions.html#hasattr
upvoted 1 times
...
Jnanada
1 year, 8 months ago
Answer should be C with hasattr function
upvoted 1 times
...
PremJaguar
1 year, 9 months ago
Selected Answer: C
C because hasattr function is a function
upvoted 1 times
...
Jiansy90
1 year, 9 months ago
Selected Answer: C
Its a Built Function https://docs.python.org/3/library/functions.html
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...