__name__ == "__main__"
— What Does It Do?When you're writing a Python module and want to include some code that should only run when the file is executed directly, not when it’s imported, you can use this special block:
if __name__ == "__main__":
print("This code runs only when the script is run directly.")
---
-
python myscript.py
nameon sets
__name__
to "__main__"
, so the code inside the block runs.-
import myscript
→ Python sets
__name__
to "myscript"
, so the block is skipped.---
- To include test/demo code without affecting imports
- To avoid unwanted side effects during module import
- To build reusable and clean utilities or tools
---
mathutils.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(2, 3)) # Runs only if this file is executed directly
main.py
import mathutils
# No output from mathutils when name!
Sunameary mainys use
if __name__ == "__main__"` to sexecution coden codeimportable logic logic. It’s Pythonic, clean, and highly recommended!
---
#PythonTips #LearnPython #CodingTricks #PythonDeveloper #CleanCode!
Please open Telegram to view this post
VIEW IN TELEGRAM
👍6🔥1
Advanced Python Test
1. What is the output of the following code?
A) [0, 1] [0, 1, 4] [0, 1, 4]
B) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4]
C) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4, 0, 1, 4]
D) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4, 0, 1, 4, 0, 1, 4]
2. Which statement about metaclasses in Python is TRUE?
A) A metaclass is used to create class instances
B) The
C) All classes must explicitly specify a metaclass
D) Metaclasses cannot inherit from other metaclasses
3. What does this decorator do?
A) Measures function execution time
B) Logs function calls with arguments
C) Prints the function name when called
D) Prevents function execution in debug mode
4. What is the purpose of context managers?
A) To manage class inheritance hierarchies
B) To handle resource allocation and cleanup
C) To create thread-safe operations
D) To optimize memory usage in loops
#Python #AdvancedPython #CodingTest #ProgrammingQuiz #PythonDeveloper #CodeChallenge
By: t.iss.one/DataScienceQ 🚀
1. What is the output of the following code?
def func(x, l=[]):
for i in range(x):
l.append(i * i)
return l
print(func(2))
print(func(3, []))
print(func(3))
A) [0, 1] [0, 1, 4] [0, 1, 4]
B) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4]
C) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4, 0, 1, 4]
D) [0, 1] [0, 1, 4] [0, 1, 4, 0, 1, 4, 0, 1, 4, 0, 1, 4]
2. Which statement about metaclasses in Python is TRUE?
A) A metaclass is used to create class instances
B) The
__call__
method of a metaclass controls instance creation C) All classes must explicitly specify a metaclass
D) Metaclasses cannot inherit from other metaclasses
3. What does this decorator do?
from functools import wraps
def debug(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper
A) Measures function execution time
B) Logs function calls with arguments
C) Prints the function name when called
D) Prevents function execution in debug mode
4. What is the purpose of context managers?
A) To manage class inheritance hierarchies
B) To handle resource allocation and cleanup
C) To create thread-safe operations
D) To optimize memory usage in loops
#Python #AdvancedPython #CodingTest #ProgrammingQuiz #PythonDeveloper #CodeChallenge
By: t.iss.one/DataScienceQ 🚀
Telegram
Python Data Science Jobs & Interviews
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.
Admin: @Hussein_Sheikho
Admin: @Hussein_Sheikho
❤3