<html><head></head><body><div class="ydp2cb02ddcyahoo-style-wrap" style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:16px;"><div dir="ltr" data-setdir="false">I'm working with an existing Python program and I've looked at the online information as to what "<span>if __name__ == '__main__':" is supposed to do. It seems a very weird and verbose way of doing anything. I'm hoping somebody can help me break that down a bit so I can understand it better.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>I get == as being the same as.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>Where __name__ and __main__ come from is a little puzzling.</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false"><span>What exactly is __main__ and why does it need __ surrounding it. Same for __name__?</span></div><div dir="ltr" data-setdir="false"><span><br></span></div><div dir="ltr" data-setdir="false">The code it's extracted from is this...</div><div dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><div><div>if __name__ == '__main__':</div><div>    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)  # Open Serial port</div><div>    try:</div><div>        while True:</div><div>            line = readString()</div><div>            lines = line.split(",")</div><div>            if checksum(line):</div><div>                if lines[0] == "GPRMC":</div><div>                    printRMC(lines)</div><div>                    pass</div><div>                elif lines[0] == "GPGGA":</div><div>                    printGGA(lines)</div><div>                    pass</div><div>                elif lines[0] == "GPGSA":</div><div>                    # printGSA(lines)</div><div>                    pass</div><div>                elif lines[0] == "GPGSV":</div><div>                    # printGSV(lines)</div><div>                    pass</div><div>                elif lines[0] == "GPGLL":</div><div>                    printGLL(lines)</div><div>                    pass</div><div>                elif lines[0] == "GPVTG":</div><div>                    printVTG(lines)</div><div>                    pass</div><div>                else:</div><div>                    print("\n\nUnknown type:", lines[0], "\n\n")</div><div>    except KeyboardInterrupt:</div><div>        print('Exiting Script')</div></div><br></div><div><br></div><div class="ydp2cb02ddcsignature">Rhys Sage</div></div></body></html>