In your example, it would give:
# morning.py
def morning_func():
print 'Good morning'
if __name__ == '__main__':
# call morning func
morning_func()
As you said calling morning like that is messy, I Suggest import morning like this:
# main.py
from morning
import morning_func as morning
then you can call morning like this:
morning()
If you wanna run morning.py
separately, as @Serge Ballesta said modify morning.py
like this:
# morning.py
def morning_func():
print 'Good morning'
if __name__ == '__main__':
morning_func()
To run the script from your python program, you can
import my_script
my_script
4) Include the following as the only high-level action statements in the script file:,We’ve packaged the action of the script as a function. The top-level action script is now wrapped in an if statement so that it isn’t executed during import.,A very small action-oriented script that uses the definitions to do useful work,What is the difference between a module and a script in Python?
import csv
import pathlib
from math
import radians, sin, cos, sqrt, asin
from functools
import partial
MI = 3959
NM = 3440
KM = 6373
def haversine(lat_1: float, lon_1: float,
lat_2: float, lon_2: float, *, R: float) - > float:
...and more...
nm_haversine = partial(haversine, R = NM)
source_path = pathlib.Path("waypoints.csv")
with source_path.open() as source_file:
reader = csv.DictReader(source_file)
start = next(reader)
for point in reader:
d = nm_haversine(
float(start['lat']), float(start['lon']),
float(point['lat']), float(point['lon'])
)
print(start, point, d)
start = point
MI = 3959 NM = 3440 KM = 6373 def haversine(lat_1: float, lon_1: float, lat_2: float, lon_2: float, *, R: float) - > float: ...and more... nm_haversine = partial(haversine, R = NM)
def analyze():
source_path = pathlib.Path("waypoints.csv")
with source_path.open() as source_file:
reader = csv.DictReader(source_file)
start = next(reader)
for point in reader:
d = nm_haversine(
float(start['lat']), float(start['lon']),
float(point['lat']), float(point['lon'])
)
print(start, point, d)
start = point
def analyze():
source_path = pathlib.Path("waypoints.csv")
def analyze(source_name = "waypoints.csv"):
source_path = pathlib.Path(source_name)
if __name__ == "__main__":
analyze()
Explain why we should divide programs into small, single-purpose functions.,Explain why we should divide programs into small, single-purpose functions. ,By giving our functions human-readable names, we can more easily read and understand what is happening in the for loop. Even better, if at some later date we want to use either of those pieces of code again, we can do so in a single line.,Now that we know how to wrap bits of code up in functions, we can make our inflammation analysis easier to read and easier to reuse. First, let’s make a visualize function that generates our plots:
def fahr_to_celsius(temp):
return ((temp - 32) * (5 / 9))
fahr_to_celsius(32)
print('freezing point of water:', fahr_to_celsius(32), 'C')
print('boiling point of water:', fahr_to_celsius(212), 'C')
freezing point of water: 0.0 C boiling point of water: 100.0 C
def celsius_to_kelvin(temp_c):
return temp_c + 273.15
print('freezing point of water in Kelvin:', celsius_to_kelvin(0.))
freezing point of water in Kelvin: 273.15
Last Updated : 21 May, 2021,GATE CS 2021 Syllabus
Output:
Geeks 4 Geeks!
In the above program, all the functions defined in calc.py are not imported.
To import all the functions defined in a Python file:
Syntax:
from file
import *
To import only required functions defined in a Python file:
Syntax:
from file
import func1, func2, func3
or modify the sys.path variable itself within a Python script.,In this file, we defined two functions print_a and print_b. Suppose we want to call the print_a function from the interpreter. We could execute the file as a script, but since we just want to have access to the function print_a, we are rather going to import it as a module. The syntax is as follows.,Functions (or other bits of code) that are called from several scripts should be written inside a module, so that only the module is imported in the different scripts (do not copy-and-paste your functions in the different scripts!).,Other interpreters also offer the possibility to execute scripts (e.g., execfile in the plain Python interpreter, etc.).
message = "Hello how are you?"
for word in message.split():
print(word)
In[1]: % run test.py
Hello
how
are
you ?
In[2] : message
Out[2]: 'Hello how are you?'
$ python test.py Hello how are you ?
import sys
print(sys.argv)
$ python file.py test arguments['file.py', 'test', 'arguments']
In [1]: import os
In [2]: os
Out[2]: <module 'os' from '/usr/lib/python2.6/os.pyc'>
In [3]: os.listdir('.')
Out[3]:
['conf.py',
'basic_types.rst',
'control_flow.rst',
'functions.rst',
'python_language.rst',
'reusing.rst',
'file_io.rst',
'exceptions.rst',
'workflow.rst',
'index.rst']