Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnkush Sharma
844 Pointsi am not understanding.Please help
when in second _app module ,i "import app,it prints both from second_app module and from app module. but when i am using dunder name method then it is not working. app module program:
def print_hello():
print("hello from the app")
print(__name__)
if __name__ == "__main__":
print_hello()
Second_app module program:
import app
print("hello from second app")
app.print_hello()
and why he in second_module use :app.print_hello()
[MOD: added ```python formatting -cf]
2 Answers
Brian Davis
1,628 PointsIs it me, or does this not make any sense? I'm not sure if he's not explaining it well, and/or, the python developers just picked a ridiculous way to engineer this topic.
Jay zhang
4,109 Pointsin second_app module the reason that he uses app.print_hello() was basically to recall the function thats in the app.py file.
its very easy to understand why you're using _ _ name _ _ == _ _ main _ . its basically just a conditional check. when you uses _ _ _name _ _ it checks the current condition of the module, ie, if you're running app.py(which contains print(_ _ name _ )) then the output would be _ _ _main _ , but if you're in second_app.py, the (print( _ name _ _).) would output the filename, which its app(no .py at the end).
KRIS NIKOLAISEN
54,971 PointsKRIS NIKOLAISEN
54,971 PointsCan you format your code using markdown?