Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    if __name__ == "__main__"

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 314 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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__":
      	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.

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

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

        Hello,

        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

        • __main__ — Top-level script environment
        • What does if \__name\_\_ == "\__main\_\_": do?

        best wishes,
        Sebastian

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

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

          Hello Herbie,

          was your question answered?

          Best wishes,
          Sebastian

          1 Reply Last reply Reply Quote 0
          • First post
            Last post