Attribute Functions

URL is provided below.
The solution is below

The exercise asks me to check whether an element of a list has an attribute “count.” What I am confused about is I thought attribute only belonged to classes and objects, not to an element of a list. But the solution seems to suggest that.

how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]

for element in how_many_s:
  if hasattr(element, "count"):
    print(element.count("s"))

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

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

if you do:

print(type("hello world"))

see how the output says <class 'string'>?

strings, lists, dictionaries are all instances of a class.

Make sense, i mean, we use method syntax/notation for methods .append() (list), or .upper() (string).

you’d have a hard time putting a non-object in a list