Understanding if __name__ == “__main__” in Python | Python Main Function with Examples

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024
  • In this Python Tutorial for Beginners video I am going to show you the Idea behind using : if _name_ == "__main__" in Python. _name_ is a built in variable in Python. the value of _name_ is set to "__main__" at runtime. Each module has a name, and this name can be used to access instructions in a module. This is especially useful in a given situation: As mentioned before, the main block of a module is executed when it is imported for the first time. But what do we do if we want the block to run only if the module was started as a stand-alone program, but not if it was imported from another module? We can do this by evaluating the attribute _name_ of the module.
    #PythonTutorialforBeginners #ProgrammingKnowledge #LearnPython #PythonCourse
    ★★★Top Online Courses From ProgrammingKnowledge ★★★
    Python Programming Course ➡️ bit.ly/2vsuMaS ⚫️ bit.ly/2GOaeQB
    Java Programming Course ➡️ bit.ly/2GEfQMf ⚫️ bit.ly/2Vvjy4a
    Bash Shell Scripting Course ➡️ bit.ly/2DBVF0C ⚫️ bit.ly/2UM06vF
    Linux Command Line Tutorials ➡️ bit.ly/2IXuil0 ⚫️ bit.ly/2IXukt8
    C Programming Course ➡️ bit.ly/2GQCiD1 ⚫️ bit.ly/2ZGN6ej
    C++ Programming Course ➡️ bit.ly/2V4oEVJ ⚫️ bit.ly/2XMvqMs
    PHP Programming Course ➡️ bit.ly/2XP71WH ⚫️ bit.ly/2vs3od6
    Android Development Course ➡️ bit.ly/2UHih5H ⚫️ bit.ly/2IMhVci
    C# Programming Course ➡️ bit.ly/2Vr7HEl ⚫️ bit.ly/2W6RXTU
    JavaFx Programming Course ➡️ bit.ly/2XMvZWA ⚫️ bit.ly/2V2CoAi
    NodeJs Programming Course ➡️ bit.ly/2GPg7gA ⚫️ bit.ly/2GQYTQ2
    Jenkins Course For Developers and DevOps ➡️ bit.ly/2Wd4l4W ⚫️ bit.ly/2J1B1ug
    Scala Programming Tutorial Course ➡️ bit.ly/2PysyA4 ⚫️ bit.ly/2PCaVj2
    Bootstrap Responsive Web Design Tutorial ➡️ bit.ly/2DFQ2yC ⚫️ bit.ly/2VoJWwH
    MongoDB Tutorial Course ➡️ bit.ly/2LaCJfP ⚫️ bit.ly/2WaI7Ap
    QT C++ GUI Tutorial For Beginners ➡️ bit.ly/2vwqHSZ
    ★★★ Online Courses to learn ★★★
    Get 2 FREE Months of Unlimited Classes from skillshare - skillshare.eqc...
    Data Science - bit.ly/2lD9h5L | bit.ly/2lI8wIl
    Machine Learning - bit.ly/2WGGQpb | bit.ly/2GghLXX
    Artificial Intelligence - bit.ly/2lYqaYx | bit.ly/2NmaPya
    MERN Stack E-Degree Program - bit.ly/2kx2NFe | bit.ly/2lWj4no
    DevOps E-degree - bit.ly/2k1PwUQ | bit.ly/2k8Ypfy
    Data Analytics with R - bit.ly/2lBKqz8 | bit.ly/2lAjos3
    AWS Certification Training - bit.ly/2kmLtTu | bit.ly/2lAkQL1
    Projects in Java - bit.ly/2kzn25d | bit.ly/2lBMffs
    Machine Learning With TensorFlow - bit.ly/2m1z3AF | bit.ly/2lBMhnA
    Angular 8 - Complete Essential Guide - bit.ly/2lYvYRP
    Kotlin Android Development Masterclass - bit.ly/2GcblsI
    Learn iOS Programming Building Advance Projects - bit.ly/2kyX7ue
    ★★★ Follow ★★★
    My Website - www.codebind.com
    DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

ความคิดเห็น • 71

  • @hammadahmed2339
    @hammadahmed2339 4 ปีที่แล้ว +21

    Best ever tutorial on name==main

  • @heaven3279
    @heaven3279 2 ปีที่แล้ว +12

    Grateful that I managed to find this after several videos, managed to get the concept straight away after watching this. Thanks for the crystal explanation and the example.

  • @erikp9014
    @erikp9014 3 ปีที่แล้ว +7

    holy shit, I spent like an hour trying to figure out what this means, but every website went into so much details that I got so confused. it was simply that __name__ was = to __main__ when running it directly from the program it was created in at first or __name __ is = to that particular file name it was created in when its imported into another program. Thank you!

  • @jojomythe
    @jojomythe 2 ปีที่แล้ว +3

    I’ve been struggling to understand this concept with all other videos, and you gave me eureka!!🎉 thank you!

  • @vijay.e4228
    @vijay.e4228 3 ปีที่แล้ว +1

    nice explaintion

  • @maulanakamal6188
    @maulanakamal6188 3 ปีที่แล้ว +1

    Very clear explanation

  • @elhipster1
    @elhipster1 3 ปีที่แล้ว +2

    perfect concise explanation for me. Thanks so much. I was very confused before. Now I totally understand

  • @cannedpineapple438
    @cannedpineapple438 3 ปีที่แล้ว +1

    Best explanation

  • @egg5474
    @egg5474 4 ปีที่แล้ว +7

    This video: Me: imports 500 libraries into Hello World.py program

  • @soliver111
    @soliver111 4 หลายเดือนก่อน

    Very clear explanation
    Thank you

  • @uditkumar7211
    @uditkumar7211 3 ปีที่แล้ว +1

    i was wandering here and there looking for precise explanation for this and you explained it very well sir thank you so much

  • @kenn1ld676
    @kenn1ld676 3 ปีที่แล้ว +1

    Great explanation of if __name__ == "__main__":

  • @travelingsandy900
    @travelingsandy900 ปีที่แล้ว

    Best explanation. Thank you very much

  • @wudassiewalle46
    @wudassiewalle46 2 ปีที่แล้ว +3

    I have watched a couple of videos on this topic and they didn't do any good except more confusion. But this explanation so clear without going around the bush. Keep us the good work. 👏

  • @dontrunjustfaceit4045
    @dontrunjustfaceit4045 10 หลายเดือนก่อน

    Good explanation. Thank you.

  • @Sam-jg5zv
    @Sam-jg5zv 4 ปีที่แล้ว +3

    Great video and clear explanation. Keep up the good work!

  • @dayanarodriguezponce5512
    @dayanarodriguezponce5512 3 ปีที่แล้ว +1

    thanks for the video you help me a lot

  • @selbimammetgulyyeva2349
    @selbimammetgulyyeva2349 3 ปีที่แล้ว +1

    Very clear explanation. Thank you!

  • @Anteater23
    @Anteater23 2 ปีที่แล้ว

    Thanks for this good video.

  • @snehan3341
    @snehan3341 3 ปีที่แล้ว +1

    thank you so much sir!!!!!!!!!!

  • @DominiqueOlaniyi
    @DominiqueOlaniyi ปีที่แล้ว

    Amazing explanation

  • @riverasanchez
    @riverasanchez 3 ปีที่แล้ว +1

    Very good explanation and example! Thank you!

  • @FelLoss0
    @FelLoss0 ปีที่แล้ว

    Amazing and concise explanation, sir. Thank you very much!

  • @ntnsve
    @ntnsve 6 หลายเดือนก่อน

    Perfect example!

  • @26_jyotigoel6
    @26_jyotigoel6 3 หลายเดือนก่อน

    best explanation😃

  • @brielleshepard
    @brielleshepard 8 หลายเดือนก่อน

    Great video! Really broke it down. Thumbs up! :)

  • @Dr_piFrog
    @Dr_piFrog หลายเดือนก่อน

    good explanation

  • @Online-rr8pk
    @Online-rr8pk ปีที่แล้ว

    Top video, so easily understandable ! Thanks a lot! 👍👍👍

  • @BrGr385
    @BrGr385 ปีที่แล้ว

    great explanation thanks a lot!

  • @Twoman-bodyweight-journey
    @Twoman-bodyweight-journey 3 ปีที่แล้ว +1

    tnx for this turtrial ,finally i understood this

  • @acronproject
    @acronproject 2 ปีที่แล้ว

    thanks for this
    💯💯💯💯💯💯💯💯💯💯💯💯💯💯💯

  • @sushank7471
    @sushank7471 ปีที่แล้ว

    well explained

  • @harivarsanpappu5142
    @harivarsanpappu5142 ปีที่แล้ว

    Great

  • @dimitrismwrakis6910
    @dimitrismwrakis6910 3 ปีที่แล้ว +1

    Excellent explanation!

  • @hotshotgolfer3079
    @hotshotgolfer3079 2 ปีที่แล้ว

    Well Done Sir. I am not confused anymore. TY

  • @omarfthoaib8749
    @omarfthoaib8749 3 ปีที่แล้ว +1

    Now it makes sense! Thanks bro...

  • @NavrozbekQosimov
    @NavrozbekQosimov 4 ปีที่แล้ว +1

    🌟Good !👍👆👏😎🌟

  • @keralatalks7435
    @keralatalks7435 2 ปีที่แล้ว

    Very clear, didnt get the proper idea, while watching other videos in youtube

  • @billseaman3103
    @billseaman3103 2 ปีที่แล้ว

    Very helpful, love you ):D

  • @nitinmaurya6835
    @nitinmaurya6835 3 ปีที่แล้ว +1

    U made it so clear that I subscribed you :D

  • @x-tremeprogrammer
    @x-tremeprogrammer 3 ปีที่แล้ว +1

    Thank man

  • @charithjeewantha
    @charithjeewantha 3 ปีที่แล้ว +1

    Thank you!

  • @arifaufdemweg9064
    @arifaufdemweg9064 2 ปีที่แล้ว

    best one

  • @theexposer9483
    @theexposer9483 ปีที่แล้ว +1

    I am checking again from you, why anyone will in first place write a statement to run the same module which has been defined above. What purpose it would serve in the first place. If have to run the module I will import it and then run the function. First I unnecessarily put code inside my module file to run the code and then put if __name__=“__main__” to prevent it from running. One reason for putting such code is to make .py file more versatile, it can be run directly without importing hence use of less memory and can be imported and run as and when required inside another application. Please enlighten us. Thanks in advance.

  • @siddanathampadmaraju4477
    @siddanathampadmaraju4477 ปีที่แล้ว

    thank you...

  • @vamsiakula9860
    @vamsiakula9860 3 ปีที่แล้ว +1

    3 blogs, 2 videos - No use
    This 1 video - Enough

  • @nepalimusician6445
    @nepalimusician6445 2 ปีที่แล้ว +3

    Watched 3 videos before this but didn't get it. Thanks for clarifying in such as a simple way.

  • @jason_huang03
    @jason_huang03 2 ปีที่แล้ว

    good job! quite explicit

  • @123arskas
    @123arskas ปีที่แล้ว

    Awesome explanation.

  • @danishuddin9752
    @danishuddin9752 2 ปีที่แล้ว

    Beautiful explanation thanq

  • @sujalsharma5181
    @sujalsharma5181 2 ปีที่แล้ว

    awesome explanation 👌cant be better 👍

  • @hirucci8986
    @hirucci8986 4 ปีที่แล้ว +2

    Thanks a lot for making many useful video and playlist tutorial like this.
    Really helpful for CS student like me.
    :)

  • @yolokaikai4071
    @yolokaikai4071 4 ปีที่แล้ว +4

    My first year lecturer spoke with that same exact ascent 😂

  • @zndxclips4810
    @zndxclips4810 ปีที่แล้ว

    thx man

  • @malakarakesh3139
    @malakarakesh3139 8 หลายเดือนก่อน

    does the spawned process also get the same __main__ value for __name__? how does this work with multiprocessing?

  • @reiyamamoto6507
    @reiyamamoto6507 4 ปีที่แล้ว +3

    What IDE do you use?

  • @USMColdies
    @USMColdies 3 ปีที่แล้ว +1

    I understand how it's used but not why there is a use for it. The return statement in a function is syntactically used to exit a function, why would there be additional statements after the return statement? Seems like bad programming, not sure how, when or why there is a use case for this...anybody knows?

  • @umeshb8210
    @umeshb8210 4 ปีที่แล้ว +2

    Can we use "from mymath import add" ?

    • @seanli75
      @seanli75 3 ปีที่แล้ว

      I think we can

  • @DeepakVerma-gy3zu
    @DeepakVerma-gy3zu 3 ปีที่แล้ว +1

    How result invalid syntax

  • @cagataytekin6372
    @cagataytekin6372 10 หลายเดือนก่อน

    buyuksun hoca

  • @aleXelaMec
    @aleXelaMec 2 ปีที่แล้ว

    great explanation, but too much underscores!!! )) its dunder methods

  • @aditya__exe
    @aditya__exe 3 ปีที่แล้ว +1

    test.py the universal name😂😂😂😂

  • @sahilapte7735
    @sahilapte7735 3 ปีที่แล้ว +1

    nice explanation