if __name__ == "__main__"

On 06/04/2016 at 17:54, xxxxxxxx wrote:

I seem to be having a hard time understanding why this line is here.

import c4d
def main() :
	doc = c4d.document.GetActiveDocument()
	print doc
if __name__ == "__main__":

I understand that this quick sample will execute the main() function. What's the difference then if the code does this?

import c4d
if __name__ == "__main__":
	doc = c4d.documents.GetActiveDocument()
	print doc

Obviously it's not going to run a main() function because now it doesn't exist.
You get the same result though.

Is one way preferred over another?
What would be the reason to use one over the other?

I feel like I'm mostly confident in programming but this seems like a glaring oversight in fundamentals on my part as to why I don't understand this.

On 07/04/2016 at 02:06, xxxxxxxx wrote:


the line " if __name__ == "__main__"" is a typical Python idiom. It is used to distinct between loading the code (in a module) and executing the code.

Using a main() function makes no different but like any other pattern it is useful to organize the code.

See also

best wishes,

On 15/04/2016 at 09:12, xxxxxxxx wrote:

Hello Herbie,

was your question answered?

Best wishes,