Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Python Basics: Identifying classes after creating a float variable

I was doing a quiz for one of the Python Basics modules and here's the question that baffled me:

In Workspaces, make a float() variable (e.g. float_a = 1.2) and use dir() on it. Which of the following is not a method of the float class?

A. conjugate B. is_integer C. as_integer_ratio D. round

Through trial and error I found out round was the correct answer because it is a built-in function. However, I don't understand the concept being explained.

What I did was type float_a = 1.2 and at the next prompt I typed dir(float_a). What came up was a long list of (what I can only assume are) methods. But all the words in the choices above were part of that list.

So in the end, I don't understand what methods are, why I used dir () (which didn't seem to be explained in any of the previous lessons) and why round was correct if that was also part of the list that came up when I used dir () on the variable I created.

Any help would be appreciate. Thanks!

Gergő Bogdán
Gergő Bogdán
6,664 Points

The dir() function lists you the list of methods and properties of the object which you pass in as parameter. you can use it a hint/help method when you do not know what you can do with an object. The ones marked with __ (ex: abs) are hidden and usually cannot be invoked, directly from the object. The __ serves a hiding hint for the python interpreter.

The round was a good answer, since it is in the list of methods returned by the dir(float_a) method, BUT it is returned as __round__, which means this is a hidden implementation of the round() method inside the float type.

so if you invoke round(float_a) that will return 1 and basically what it does it invokes the __round__ method on the float_a variable.

So the result of these 2 methods are the same:

float_a = 1.2

# these 2 method invocations are the same
round(float_a)      # will return 1
float_a.__round__() # will also return 1

2 Answers

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You found the answer the right way. And, like Gergo Bogdan pointed out, float only has .__round__() and not .round(). Still, I'll put in something more easily picked out.

Thank you both for your explanations! Appreciate the quick replies.