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

Community Tips and Resources

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!

17 posts were split to a new topic: Why is count in quotes and how does hasattr work in an if statement?

6 posts were merged into an existing topic: Why is count in quotes and how does hasattr work in an if statement?

3 posts were merged into an existing topic: Why is count in quotes and how does hasattr work in an if statement?

A post was split to a new topic: Object types are classes with attributes

6 posts were split to a new topic: How to count the “s”?

2 posts were merged into an existing topic: Why is count in quotes and how does hasattr work in an if statement?

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.

https://docs.python.org/3/library/stdtypes.html#string-methods

In this case, the objects in how_many_s that have the count method are: str & list. Some other methods for str would be upper, find, and lower.

I hope someone finds this useful.

26 Likes

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".
image

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?

Thanks for any help.

They are both attributes. Think in terms of objects, which pretty much everything is in Python.

There is no such attribute, "str".

 hasattr(object, 'count')

A str object will have a count attribute (method) since it also has an __iter__ attribute (meaning it is an iterable).

>>> dir(str)
[..., '__iter__', ..., 'count', ...]
>>>

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.

1 Like

You’re welcome!

One way is to check its type…

>>> type(str.count)
<class 'method_descriptor'>
>>> 

Ah, I see. If so, then hasattr doesn’t tell you a whole lot, does it?

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!

1 Like
>>> 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.

>>> 
1 Like