0000

ifmain

10 Jul 2021 • viridi | History | Comment

Learning first part of ifmain to taste the DRY (don’t repeat yourself) principle [1].

codes

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()

results

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.

notes

  1. Louis de Bruijn, “Write Better Python Scripts”, BetterProgramming, 26 May 2020, url https://betterprogramming.pub/ce58c1ebf690 [20210710].

 

introductionrefs