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

help() 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
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

In 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.

oh 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
Chris Freeman
Treehouse Moderator 68,468 Points

Correct, but they can be referenced directly but are usually done in speciall circumstances. For example, when checking to see if a .py file 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:

def main():
    '''Program to run if this is the top level module'''
    print("Starting top level program")

if __name__ = "__main__":
    # if this is the top module, call main
    main()

Now if you run python mycode.py with the contents above, main() will execute. but if you import mycode it 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 through super(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.