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

I am still not exactly sure what __init__ does

I still don't get what init does can someone please explain

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

__init__ is called to set up a new instance of an object, whenever one is created. For example:

class Something():
    def __init__(self, newValue):
        self.value = newValue

newObject = Something("here's some text")
newObject.value
>>> "here's some text"

To explain, we've defined a new class called Something, and we've written an initializer for it that takes in some data and sets it to a property called value on the new instance. After that, we've created a new instance of Something and given it a string "here's some text", and we've assigned that new Something instance to a variable called newObject. When we read what data is in newObject.value, we can see that it's the string "here's some text" like we passed into Something's initializer

that sounds about right