• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding where does Python main method come in the program flow as that is not the entry point?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learnt that in Python, main method is neither the entry point , nor mandatory. From the top, execution will happen line by line. So, where exactly does the main method come in the flow ? Does it happen when it reaches line by line upto the main method line ? Thanks
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no main method in Python. It is a procedural language with OO pretensions. So when you run a program: "python path/to/program.py", Python simply starts at the first executable statement in the file and works down.

Common practice, however, even when not using Python in OOP mode is to put the bulk of the procedural code in a function and then have the option to execute that function like so:


This technique is especially useful if you have a program module that's normally invoked from somewhere else, but you want to be able to run it independently as a unit test.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.So does that mean that this main method (even if present) is like any other method and does not have any special meaning ? What does the third line( if statement) do and from where does the value of __name__ variable come from?
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't call it a method, because methods are object-oriented. It's just a function. And the name is, as you surmised, not important.

As for the conditional at the end, see: https://docs.python.org/3/library/__main__.html

double-underscores indicate python internal elements, incidentally.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The link says that main is the name of scope.I was thinking that it is the name of function. Also, it seems that main has a special meaning. I will read more about this to understand these details but at this point of time I know that the effect of the below code is that it calls main if it is called as a scirpt and not so if it is imported.



 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:



What I understand is that main which is the function name can be anything as it is the name of function.However __main__ is having special meaning as when program is run as a script __name__ automatically gets popupated as __main__. I will play around this on an IDE to understand more on this.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic