7. Dunder Main (17m) Flashcards
There is two python files:
app.py containing the following code:
~~~
print(“hello from app.py”)
~~~
and dm.py containing the following code:
~~~
print(“hello from dm.py”)
~~~
Q1. How will you import dm.py into app.py?
Q2. What is Dunder main in python and how will you add it to dm.py?
To import dm.py into app.py, you can use the import statement. Here’s how you can do it:
In app.py:
~~~
import dm
print(“hello from app.py”)
~~~
In dm.py:
~~~
print(“hello from dm.py”)
~~~
Now, when you run app.py, it will import dm.py and execute the code in both files. The output will be:
~~~
hello from dm.py
hello from app.py
~~~
As for your second question, “Dunder main” refers to the __main__ special variable in Python. It is used to check if a module is being run directly as the main program or being imported as a library.
To add a __main__ block in dm.py, you can use an if __name__ == “__main__”: condition. Here’s an example:
In dm.py:
~~~
print(“hello from dm.py”)
if __name__ == “__main__”:
print(“This is being run directly as the main program”)
~~~
Now, when you run dm.py directly, it will execute the code within the if __name__ == “__main__”: block. The output will be:
hello from dm.py This is being run directly as the main program
However, if you import dm.py into another module, the code within the if __name__ == “__main__”: block will not be executed. This allows you to have code in dm.py that will only run when dm.py is executed directly, but not when it’s imported as a library by another module.
What does this mean in python:
if _ _name_ _== "_ _main_ _":
?
The if __name__ == “__main__”: condition is a common idiom used in Python to check if a script is being run directly as the main program or if it is being imported as a module.
Here’s what it means:
When a Python script is executed directly, the __name__ variable is set to the string “__main__”. This indicates that the script is the main program being run.
However, when a script is imported as a module by another script, the __name__ variable is set to the name of the module instead of “__main__”.
So, the condition if __name__ == “__main__”: allows you to specify code that should only run when the script is executed directly as the main program and not when it’s imported as a module.
For example, consider the following code in a file named example.py:
~~~
def hello():
print(“Hello, World!”)
print(“example.py’s __name__:”, __name__)
if __name__ == “__main__”:
print(“This code will only run when example.py is executed directly”)
hello()
~~~
When you run example.py directly, you will see the following output:
~~~
example.py’s __name__: __main__
This code will only run when example.py is executed directly
Hello, World!
~~~
However, if you import example.py into another module and run that module, the code within the if __name__ == “__main__”: block will not be executed. The output will be:
~~~
example.py’s __name__: example
~~~
This allows you to have code in a script that will be skipped when the script is imported as a module, but will run when the script is executed directly. This can be useful, for example, for including test code or examples that you want to run only during development or when the script is used as a standalone program.
There are two Python files:
- app.py with following code:
~~~
import dm
print(“hello from app.py”)
dm.list_names()
~~~ - dm.py with following code:
~~~
def list_names():
print(“This is a list of names: 1.Mickey, 2.Naruto, 3.Goku”)
if __name__ == “__main__”:
print(“hello from dm.py”)
print(“__name__ is: “ , __name__)
list_names()
~~~
Explain both
The provided code consists of two Python files: app.py and dm.py. Let’s go through each file’s code and explain their functionality.
app.py:
* In app.py, the dm module is imported using the import statement. The code then prints the message “hello from app.py”. After that, it calls the list_names() function from the dm module.
dm.py:
* In dm.py, there is a function list_names() that prints a list of names. Additionally, there is an if __name__ == “__main__”: block that checks if the script is being run directly as the main program.
* When dm.py is executed directly, the code inside the if __name__ == “__main__”: block will run. In this case, it prints the message “hello from dm.py” and the value of __name__ (which will be “__main__”). It then calls the list_names() function.
* However, if dm.py is imported as a module by another script (like app.py), the code inside the if __name__ == “__main__”: block will not run. This allows the module to contain code that should only run when the module is executed directly as the main program.
* When you run app.py, it imports dm.py and executes the code in app.py. The output will be:
~~~
hello from dm.py
__name__ is: __main__
This is a list of names: 1.Mickey, 2.Naruto, 3.Goku
hello from app.py
This is a list of names: 1.Mickey, 2.Naruto, 3.Goku
~~~
As you can see, the code inside the if __name__ == “__main__”: block of dm.py is executed when dm.py is imported as a module by app.py.
Write code to find the word in a
Challenge: Two python pages with each one having different challenge.
Page 1: alphabet.py
~~~
## Challenge ##
# list that would come first alphabetically
Example: When given the list below
# word_list = [“hamster”, “turtle”, “cat”, “bird”]
# The code should find “bird” as the word that
# would come first alphabetically
~~~
Page 2:
~~~
import alphabet
word_list = [“hamster”, “turtle”, “cat”, “bird”]
last_names = [“Rodriguez”, “Lee”, “Yakinian”, “Amendola”]
alphabet.func_name(word_list)
# alphabet.func_name(last_names)
~~~
Now do the challenge and write codes for both pages.
For alphabet.py the code is:
~~~
def find_lower_letter(list_of_words):
first_word = list_of_words[0]
for word in list_of_words: if word < first_word: first_word = word print(first_word)
if __name__ == “__main__”:
word_list = [“hamster”, “turtle”, “cat”, “bird”]
find_lower_letter(word_list)
~~~
The above is one way, alternative we can also use min function and get the same done with the following code:
For alphabet.py the code is:
~~~
def find_lower_letter(list_of_words):
first_word = min(list_of_words)
print(first_word)
if __name__ == “__main__”:
word_list = [“hamster”, “turtle”, “cat”, “bird”]
find_lower_letter(word_list)
~~~
For app.py the code is:
~~~
import alphabet
word_list = [“hamster”, “turtle”, “cat”, “bird”]
last_names = [“Rodriguez”, “Lee”, “Yakinian”, “Amendola”]
alphabet.find_lower_letter(word_list)
alphabet.find_lower_letter(last_names)
~~~