mirror of
https://gitea.com/Lerking/python-crash-course.git
synced 2026-01-11 11:07:08 +01:00
if __name__ lesson. /JL
This commit is contained in:
8
lesson_1/hello_world.py
Normal file
8
lesson_1/hello_world.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import os
|
||||
|
||||
def main() -> None:
|
||||
print("Hello world!")
|
||||
print("From: ", os.getcwd())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
25
lesson_1/launching_python_file.py
Normal file
25
lesson_1/launching_python_file.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
How to launch a python script, the right way
|
||||
"""
|
||||
|
||||
# This is a very short introduction into the correct way of launching a python script.
|
||||
# There's only 1 correct way of doing this, shown below.
|
||||
|
||||
import os
|
||||
|
||||
def main() -> None:
|
||||
print("Hello world!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
# In short a python script can be devided in 3 parts.
|
||||
# Imports, definitions, directly executable.
|
||||
# Only your main file should contain these 3 parts/sections. Any imported file should only contain the first 2
|
||||
# parts. This prevents you from running a file that's only supposed to be imported, directly from terminal.
|
||||
# Your main file must follow this setup. Failing to do so, will give you issues further down the path.
|
||||
|
||||
# The overall purpose of this setup is that the file you execute with python, is called "__main__" and
|
||||
# any other files is called by their import name.
|
||||
|
||||
# Any code definitions are not executed until you actually call the function/method.
|
||||
Reference in New Issue
Block a user