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
Michael Pastran
4,727 Pointshelp() function help!!
okay so here is my question. whenever i use the help() function in python. i always see things like __ add __ , __ contains __ (these are for strings). but my question is. what do the __ mean? like what is supposed to go there?? is that supposed to be our variable in the first __ and then in the second __ the arguments for the function? any help would be awesome. thanks
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsIn short, it's a naming convention. Attributes that start and end with double underscores ("dunders") are not intented to be manipulated by the user. There are, as much as python can, private attributes and method of that class. You can also see a list of all attributes using the dir() function.
As it says in the PEP-8 document: __double_leading_and_trailing_underscore__ are for "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__ , __import__ or __file__ . Never invent such names; only use them as documented.
Michael Pastran
4,727 PointsMichael Pastran
4,727 Pointsoh okay, so what i get from that is that they are like any other method right? they just happen to be written like that because they are not intended to be manipulated or change by the user. thanks btw and yeah thats where i see them all the time. in the dir() but i just didnt know what they were. thanks chris
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsCorrect, but they can be referenced directly but are usually done in speciall circumstances. For example, when checking to see if a
.pyfile was invoked directlly or was imported into another program you can test__name__. Python sets the value of this attribute to "__main__" when a module is the top level. Using this in code:Now if you run
python mycode.pywith the contents above,main()will execute. but if youimport mycodeit will not.In some cases you will be creating your own double-underscore method which don't change existings one, but rather supercede them or work in conjunction with existing dunder functions. For example, you will likely define an
__init__()function as part of your classes which may also refer to the__init__()of its parent class throughsuper(ThisClass, self).__init__(). You may also be creating__str__()and__repr__()methods for your classes.These details are covered in Treehouse Python courses you take.