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 Controlling Conversion

Jay Norris
Jay Norris
14,824 Points

Conversions: Instance into dictionary

I know Kenneth stated that converting an instance to a dictionary was outside the scope of this course, but I would be really curious to see some examples.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's honestly really simple. instance.__dict__.

But you'll eventually want to control that conversion (I don't suggest you actually do this, though).

In that case you'll need to override the __dict__ method, make it a @property, and return whatever dictionary you want to return.

Again, though, don't do this