I think I’ve finally understood a little about this seemingly cryptic line of code, but on hindsight it is just what it means – that whether the ‘name’ of the code that is running (i.e. the .py file) is the ‘main’ programme that is running.
First, create a file named ifmaintesteg.py, with the following block of code:

Next, create another .py file (doesn’t matter what we name it here, but let’s call is ifmaintesteg_2.py) with just one line of code, importing the file created earlier ( ifmaintesteg.py ) as a module. This imported module has to be of the same name as the file earlier, also both .py files have to reside in the same folder.

When the file ifmaintesteg.py is run, each and every line of code in the programme will be executed. As such Line 10 calls the print_prog() function defined in Line 3 to 4, pauses for five seconds (Line 11), then calls the print_import() function, followed by another pause of five seconds. The printout is shown below.

When the file ifmaintesteg_2.py is run, the only line of code therein imports ifmaintesteg.py into the ‘main’ programme. However, the code nestled within the if conditional (Line 10 and 11) is not executed, simply because now the name of the ‘main’ programme is ifmaintesteg_2.py, not ifmaintesteg.py. Hence, only Line 13 and 14 is executed, with the output shown below.

Excellent article for everyone who are looking for the piece of information about Python.we are also into same track for more info visit our website python page!
LikeLike
Thank you!
LikeLike