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
hector villasano
12,937 Points__init__ ? Can some one explane this tome
I don't understand why you would use this?
2 Answers
Brandon Wall
5,512 PointsFrom the documentation that i've read, Python has no explicit constructors and destructors that you would find in other languages, but __init__ is the closest thing you'll get in Python to a 'constructor'. Basically this 'magic' method (they're called magic methods, the double underscore ones, google 'python magic methods' there are many different ones you can over-load to change the behavior of an object) when defined on a class lets you define how the class is 'constructed' when it is instantiated. So instead of having a class with static attributes, or having to explicitly and separately call a function, you can define certain things in the __init__ method, so when the class is first constructed/an instance is created, all of the things in the __init__ method will happen automatically. The best lesson I saw using a lot of stuff defined in __init__ was the workshop on tkinter. Tkinter lets you build graphical user interface applications with python and Kenneth wrote a large part of that example program in __init__ so that as soon as the script is run, everything in __init__ automatically executes and builds the window with the stuff in it.
Sorry that's a lot and i probably didn't explain it very well but I tried :D
sorry the underscores aren't showing up for some reason, i need to wrap them in backticks i think but you know what i mean.
[Edit: backticks added -cf]
Brandon Wall
5,512 PointsDid I earn a best answer? :D
And thank you to the mod that fixed the formatting of my answer, you're awesome!
Chris Freeman
Treehouse Moderator 68,468 PointsMy pleasure!
hector villasano
12,937 Pointshector villasano
12,937 PointsThanks for the knowledge. I learned a lot from your paragraph !
=]