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

Why does self.[::-1] return the reverse of the string when used in defining the class?

I thought [::] was used for lists only?

1 Answer

Hi there! Slicing in python is a tool used for ordered collections. So strings, lists, tuples can all be sliced because they have an order. unordered collections like sets and dictionaries can't be sliced by index. What's unique about lists in python's ordered collections is that they can be changed without creating an new instance of the collection - they are said to be mutable. Strings and Tuples are immutable - you can slice them to get values from them, but if you wanted to change them in anyway you have to make a new string/tuple.

Hope it helps

Ah yes. I was thinking it was for mutable objects only. Thanks for clearing that up!