FAQ: Learn Python: Classes - Attribute Functions

This community-built FAQ covers the “Attribute Functions” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Attribute Functions

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can someone explain why "count" in the if hasattr(element, "count") line of this problem does not return false?

The way I am understanding, hasttr() is that it takes in 2 arguments and it checks if the 2nd argument exists and returns True if it does.

Shouldn’t "count" return False and skip the print statement?

I probably just need some sleep but would really appreciate someone helping me understand why if hasattr(element, "s") doesn’t work.

Thank you. :slight_smile:

image

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/data-types/exercises/attribute-functions

2 Likes

Even thought the count is in " " it’s not checking for the string count. It’s using the count method. In this case it just checking to see if the item is a string.

This attribute thing hasn’t made any sense to me. Incoming really long rant/confusion:

First of all, both "count"s in the solution function in ways that the course has not taught, so I couldn’t get the answer right because they were never explained.

(Side Note):
This is by biggest complaint about this course is being occasionally roadblocked by being asked to do something that hasn’t been taught how to do, and I have to reverse learn it from looking at the solution (like what “.index” does in module 4. (Fortunately a lot of it is self-explanatory after-the-fact so I can still learn it)

Anyways, looking at the “.count” in the print() statement, I can infer that it probably counts the number of "s"s in element. But it was really confusing at first after just coming from learning about methods in classes and and calling them. So I wasn’t sure if “.count” was a built-in function of Python or calling a method that was hidden or something.

Secondly, from what dataace74980 was saying, it sound like the first “count” checks whether each item is in the list is a string or not and also somehow it’s using the “count method” at the same time? Now I’m really confused, and on top of that, the solution prints “5” and “2” (which I assume are counted by the “.count” function"). So it looks like it counted the number of "s"s in the string as well as in the list. That tells me that the first “count” isn’t checking whether something is a string or not because the 2 "s"s are from the list which isn’t classed as a string (and there isn’t a second loop looking at the list). This also makes me wonder why it did not include the “s” from the dictionary. So what the heck does the first “count” do?

Any explanation of this would be greatly appreciated.

8 Likes

@triangularapple @netrunner25041
Consider this list -
how_many_s = [{‘s’: False}, “sassafrass”, 18, [“a”, “c”, “s”, “d”, “s”]]
we are looping through individual element of these list.
First Iteration -
element = {‘s’: False}
Q) Does this dictionary element is having inbuilt function count(argument) !?
Ans = We can check it using hasattr(element, “count”) function . It will return false as Dictionary does not have in-built method / function count() .
You can check and confirm in-built methods of dictionary here (https://www.programiz.com/python-programming/methods/dictionary)

Second Iteration -
element = “sassafrass”
lets check whether String in-built methods/function have in-built count() method.
You can confirm from here . (https://www.programiz.com/python-programming/methods/string)
Yes, String has in-built count method, so the hasattr(element, “count”) will return true, and next line will be executed.
Next line is print(element.count(“s”)) - As In second iteration element’s datatype is string, it will call string in-built method count(argument) and it will count occurrences of character/letter of that string.

In these way, whole list will be looped and executed.
Hope these helps.

10 Likes

The line in question returns “False” in response to the if statement, so it doesn’t print anything (including “False”). Since the if statement only says to print element.count(“s”) if the attribute is present, there’s no need to print False.

My solution followed by your solution – both work correctly, and neither says to print False:

for s in how_many_s:

  try:
    print(s.count("s"))
  except AttributeError:
    None
    
for element in how_many_s:
  if hasattr(element, "count"):
    print(element.count("s"))

# prints True or False    
for element in how_many_s:
  print(hasattr(element, "count"))

Hello!

Are methods considered attributes? When we use the hasattr() function, it seems to find the “count()” method, but I can’t wrap my head around why it would do this, since I’ve been under the impression that attributes and methods are separate concepts…

Thanks in advance!

Totally didn’t understand this lesson: In your example

I don’t get the line

if hasattr(element, "count"):

Googling I found this python code:

class Person:
    age = 23
    name = 'Adam'

person = Person()

print('Person has age?:', hasattr(person, 'age'))
print('Person has salary?:', hasattr(person, 'salary'))

first is True and second is False…

It makes much much more sense for me like this and it’s in the context of classes.
(We are checking in the Person class for a age attribute)

2 Likes

+1 exactly same rant for me

Yes they are. Attributes are associated objects, and methods are associated objects so that makes them attributes.

We’re given a list, and lists are made up of zero or more elements, which we are iterating over. element refers to a value from the list.

count is a method of some objects, but not of others. For instance, dict and int objects do not have a count() method, and therefore no count attribute. Strings and lists do have that attribute.

hasattr is a built in function for testing whether objects have a particular attribute, or not.

When we test either the string element or the list element in the given list, we find that they do have a count attribute so we can call that method of them to count the s's in their value.

if hasattr(element, 'count'):
    print (element.count('s'))

Note that we are calling the method on the element. Without this check in place, we would raise an exception on the dict object, right off the top.

2 Likes

Tnx this was a helpful explanation!

2 Likes

Can someone help me understand why count needs to be in parenthesis for this exercise? My understand is that the line of code is cycling through the list and seeing if the variable is able to ‘count’ and if so count the number of ‘s’ in the variable. If that’s the case why put ‘count’ in a string vs. referring the actual function count?

Do you mean this line?

if hasattr(element, 'count'):

We do this to verify that the element has a count method, as some do not. If we try to run a method that is not an attribute, then it will raise an exception, halting the code.

Hi Roy - thanks for the response. I understand that we’re checking to see if the element has a count method but i’m still not 100% sure why we just don’t search for count vs. ‘count’ . For some reason i thought if you are searching for count as a string (i.e. with quotations). For example you would use a piece of code like this: print(“The count is:”, count) not print(“The count is:”, ‘count’)

Sorry, you’ve lost me. What are we searching for? Where does count come into this?

Hm, I think your response may have shed some light on my confusion…but perhaps not. There is a count method in python that counts stuff correct? I believe in this exercise what the “”“f hasattr(element, ‘count’):”"" line of code is doing is checking to see if each element in the list has the ability to count and, if so, count how man times ‘s’ is in each element. Is that right? Or in this case is the ‘count’ used in this exercise and the general count method entirely different?

We’re aimed at determining if a class of object has a count attribute. If not, then we shouldn’t call that method on that object.

>>> (1).count(1)
Traceback (most recent call last):
  File "<pyshell#103>", line 1, in <module>
    (1).count(1)
AttributeError: 'int' object has no attribute 'count'
>>> 
>>> hasattr(1, 'count')
False
>>> 
>>> [1].count(1)
1
>>> "1".count('1')
1
>>> {"1": 1}.count('1')
Traceback (most recent call last):
  File "<pyshell#108>", line 1, in <module>
    {"1": 1}.count('1')
AttributeError: 'dict' object has no attribute 'count'
>>> {"1": 1}.count(1)
Traceback (most recent call last):
  File "<pyshell#109>", line 1, in <module>
    {"1": 1}.count(1)
AttributeError: 'dict' object has no attribute 'count'
>>> 

What we 'search" for doesn’t change anything if the method does not exist.

+1 on the rant for this question.

Extremely confusing with the whole ‘count’ attribute thing. I realize now that the element itself (via it’s data type) has the ‘count’ attribute, but still, that’s after 20 mins of Googling.

This question needs to be rewritten.

There are a few things I don’t understand about this lesson.

  1. Why is the attribute you’re looking for with hasattr() passed as an argument in quotation marks? So far we’ve used quotation marks with strings, so it’s not intuitive to use them when looking for a method.

  2. Since an attribute can be either a method or a variable, when we look for the "count" attribute using hasattr() and it comes back as True, how do we know it is a method that can be called and not just a variable of the class? Is it considered best practice to not have a variable and a method in the same class that both have the same name?

Because attribute names are type str.

The method only checks for attributes of a sequence class (str, list). It is up to us to determine (or know already) that the attribute is a method.