Learning first part of ifmain
to taste the DRY (don’t repeat yourself) principle [1].
First file is hello_world.py
"""
hello_world.py
"""
def printhello():
print('Hello, World! -- import')
def main():
print('Hello, World! -- run')
if __name__ == '__main__':
main()
and second file is import_hello_world.py
"""
import_hello_world.py
"""
from hello_world import printhello as ph
ph()
The files can be executed as follows with the results
L:\home\butir\code\0\07>py hello_world.py
Hello, World! -- run
when we execute the first file and
L:\home\butir\code\0\07>py import_hello_world.py
Hello, World! -- import
when we execute the second file. The last part of hello_world.py
is responsible in call the appropriate function.