<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body dir="auto"><div dir="auto">It's just python's way of saying "do this if you're calling this module in order to get started". Otherwise, you're just going to call the functions/methods in the module in the normal way.</div><div dir="auto"><br></div><div dir="auto">That might not be the clearest or most accurate explanation, but I don't think it's far off.</div><div dir="auto"><br></div><div dir="auto">Peter</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><br></div><div id="composer_signature" dir="auto"><div style="font-size:12px;color:#575757" dir="auto">Sent from my Galaxy</div></div><div dir="auto"><br></div><div><br></div><div align="left" dir="auto" style="font-size:100%;color:#000000"><div>-------- Original message --------</div><div>From: Rhys Sage via Swlug <swlug@mailman.lug.org.uk> </div><div>Date: 26/02/2022 14:46 (GMT+00:00) </div><div>To: swlug@mailman.lug.org.uk </div><div>Cc: Rhys Sage <rhys_sage@yahoo.com> </div><div>Subject: [Swlug] if __name__ == '__main__': </div><div><br></div></div><div style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:16px;" class="ydp2cb02ddcyahoo-style-wrap"><div data-setdir="false" dir="ltr">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 data-setdir="false" dir="ltr"><span><br></span></div><div data-setdir="false" dir="ltr"><span>I get == as being the same as.</span></div><div data-setdir="false" dir="ltr"><span><br></span></div><div data-setdir="false" dir="ltr"><span>Where __name__ and __main__ come from is a little puzzling.</span></div><div data-setdir="false" dir="ltr"><span><br></span></div><div data-setdir="false" dir="ltr"><span>What exactly is __main__ and why does it need __ surrounding it. Same for __name__?</span></div><div data-setdir="false" dir="ltr"><span><br></span></div><div data-setdir="false" dir="ltr">The code it's extracted from is this...</div><div data-setdir="false" dir="ltr"><br></div><div data-setdir="false" dir="ltr"><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>