Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.

Similar presentations


Presentation on theme: "Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then."— Presentation transcript:

1 Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then the main aspect of the program occurs ie referral to functions and use of if elif etc The last section of your program that i would like you to get used to is : "if name == ‘main’" in a Python program do?

2 Lets create an example to demonstrate how it works

3 Step 1 –create a python file named mathsconstants.py Step 2 –type the following code in it

4 Step 3 –create another python file called inaccuratemaths.py Step 4- type in the code below into inaccuratemaths.py

5 When we execute or run When this is executed using python inaccuratemaths.py, the value of PI is imported from our mathsconstants.py file and brought into the (namespace of)inaccuratemaths.py As you might expect, the result is: The circumference of a circle with radius 10 is 62.857142857142854

6 Carrying out testing on python module mathsconstants.py Now consider that we might want to carry out some testing in our Python module above called mathsconstants.py We might want to add some testing or debug code so that we assure ourselves that the ‘correct’ value is being computed for PI: Type in the following code

7 And when you run it……. Although we only intend our mathsconstants.py code to be imported into other programs, for testing purposes we can run it directly using python mathsconstants.py. When we run it, we get something like: All good in the hood. Our testing code shows that we’ve got the ‘correct’ value for Pi, and we can see that our mathsconstants.py file is working correctly

8 What will the impact be of that print() function that we’ve added for testing purposes? we have forgotten that the mathsconstants.py file is also being imported into inaccuratemaths.py program. When a Python program is imported into another one, it is effectively executed as it is imported. So the from mathsconstants import PI line, imports the mathsconstants.pyfile and executes it as it goes. Which means it will find the print() function that we added, and execute that too.

9 If we now run our inaccuratemaths.py program, we will get this output: You can see that the print() function from mathsconstants.py is being executed. Our testing/debug code is being run when we didn’t want it to be. What we need is a way to distinguish if our code is being executed as part of an import statement, or if it being executed as a standalone program. Python’s very cryptic solution is to check it’s internal name of the source code file. If it’s being executed as a standalone program, Python will set its __name__ reference to be equal to the string __main__

10 However, if the Python code is being imported, the __name__ reference will be set to the name of the source file without the “.py” ending - mathsconstants in our example. So to fix this, we can add the following code to our mathsconstants.py program so that our testing code is not run if the file is being imported, but is executed only if we run the program from the command line: now if we execute our inaccuratemaths.py code, we will no longer get the testing output from our mathsconstants.py code: Now save the maths constants program with the if_main now in it then run the inaccurate maths program

11 And finally……….. So what using if_main does is allow the function created in maths constant and enable it to be imported and used as many times as you want. what it will do is allow the reference to the data in the program but it will not execute the program and print the data. So in inaccuratemaths.py program result of running the program will be You will notice it doesn’t print out the print statement in the referral program mathsconstant.py It essentially enables an absolute reference point If you don’t use if_main what will happen is the program you have created called maths constant will print What you want is only reference to the part of the program called pi To do that you must use if_main otherwise when you import a python program it will assume you want to execute or run all of the code in the program


Download ppt "Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then."

Similar presentations


Ads by Google