Why is count in quotes and how does hasattr work in an if statement?

I think folks are forgetting our lesson on dir().

When we query hasattr() in this lesson, we aren’t looking for anything represented on screen. That is, we aren’t looking for count in how_many_s. We are actually looking for count in the attribuest of element[index] in how_many_s.

we see on screen:

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

or, more visually understandable:

how_many_s = [
{‘s’: False},
[“a”, “c”, “s”, “d”, “s”]

Python sees:

how_many_s = [ <dict>, <str>, <int>, <list> ]

The effect of hasattr(element, "count") is that Python searches dir(element)' for count`:

dir(<dict>) looks like this:

['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

As we can see, a dictionary has no count attribute/method and False is returned.

Meanwhile, dir(<str>) looks like this:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

As we can see, a string does in fact have an attribute/method called 'count' as it’s 4th non-dunder method.

This same process is effected on any user-created Class and can be seen by calling dir(UserCreatedClass) to see what hasattr() will be iterating through.

i think this will help to understand what is exactly happening.

first we are taking elements of the list ‘can_we_count one’ by one with the help of ‘for loop’.
As you can see first element we have is a ‘dict’ that doesn’t have attribute ‘count()’ so it will give a return ‘false’.
in the next iteration we have a ‘string’ which have in-build ‘count()’ attribute so it will give a return ‘true’
next iter will have an ‘int’ element which doesn’t have ‘count()’ attribute
and so on…

1 Like

thankyou for your time :heart_eyes: