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 Object-Oriented Python Advanced Objects Subclassing Built-ins

reversedstr.Reversedstr('hello) ??

Why can't we just call Reversestr('hello') since Reversedstr is already a class ?? what is the purpose of the lower case reverdstr(is that the module ?)

1 Answer

Alex Koumparos
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Quyen,

It all depends on how we import the class. In the video, Kenneth imports ReversedStr into the local namespace (around 1:50):

from reversedstr import ReversedStr

Because the ReversedStr class is now in the local namespace, it can be called directly:

rs = ReversedStr('hello')

In contrast, at around the 3:35 mark, Kenneth just imports the module:

import reversedstr

In this situation, all the module’s classes remain in that module’s namespace (which is generally better practice as it ensures we don’t have name collisions).

Because the ReversedStr class is in reversedstr’s namespace, we need to prefix ReversedStr with the module name every time we access it (obviously this can get tedious for methods we use a lot, which is why the ability to load the class into the local namespace exists):

rs = reversedstr.ReversedStr('hello')

Here’s a link to an article that explains this in more detail.

Hope that helps,