I’ve had much difficulty trying to comprehend this lesson. My understanding came after I figured out that different object types such as stings, lists, and dictionaries are themselves classes that contain methods.
I don’t think this exercise works. As far as I can tell, hasattr(element, "str") checks for an attribute, not a method, where "str" is the attribute. However, the exercise asks you to:
“check if the element has the attribute count. If so, count the number of times the string "s" appears in the element”
In the pydocs, count is a method, which makes sense to use in this case.
Additionally, the hint indicates that you should be checking and using a "special_method", not a "special_attribute".
It seems to me that this exercise takes advantage of the fact that the String class happens to have a separate count attribute and method.
What is up? Are methods and attributes the same thing? Does hasattr() also check for methods?
That explains a lot, thanks! I’m much more used to OOP in Java, so all of this is super hard for me to wrap my head around.
Since hasattr checks for class variables, instance variables, and methods, as all three are attributes, if hasattr(object, 'count') evaluates to True, how can we know if object.count or object.count() is the valid statement? Since hasattr(object, 'count') would evaluate to True if count was a method or a variable, but one of the statements would throw an error.
Which is why we have multiple tools to work with. hasattr is an easier way to test for attributes than scanning through a dir() list, and it has a simple purpose as a predicate function that can be called dynamically.
I guess you’re right. I just have to do more programming with python to wrap my head around this :). One final question (I promise). Does a dir() list contain attributes or only methods?
All attributes, which includes methods. As we’ve seen, a second test would be needed to determine if the attribute has a value and is not a method. All the dir() list contains is str objects, the names given each attribute of the class.
I think this exercise was just quite poorly written, as it talks about hasattr and getattr in the context of variables, and I don’t believe the lesson mentions before that variables and methods are attributes, which isn’t the way I would think coming from Java. This makes a lot more sense to me, thank you so much for your help!
>>> help(getattr)
Help on built-in function getattr in module builtins:
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
>>>
>>> help(hasattr)
Help on built-in function hasattr in module builtins:
hasattr(obj, name, /)
Return whether the object has an attribute with the given name.
This is done by calling getattr(obj, name) and catching AttributeError.
>>>
One suspects that were it possible, it would have a count attribute. However, integers are not subscriptable, as in they cannot be split so a count attribute is meaningless.
We can morph a class into a custom class and then give that class any attributes we like, even count, though it would need a little help.
>>> class Number(int):
def __init__(self, n):
self.n = n
def count(self):
return len(str(self.n))
>>> a = Number(9999)
>>> a.count()
4
>>>