Best Practices for Python Main Functions

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ธ.ค. 2024

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

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

    Best explanation I've ever seen on TH-cam about Python.
    It's impossible to explain it more clear than that.
    Congrats!

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

    I've bought quite a few e-courses - not in vain I've learned a lot - but none of them has come even close to explain main like you; rather confused me! As another one wrote, this is pure gold. Now I not only understand the main function I even see how, and why, it will be quite an implement - a great tool - in my current ongoing projects. Thank you so much!

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

    Masterfully explained. I loved that you reviewed the learning objectives at the end, but with a more in-depth explanation. Fantastic, easy to understand. Thanks.

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

    That was an excellent explanation! I've always wondered whether to put my main code in the conditional or actually use a separate "main" function. Thanks for explaining it!

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

    Best explanation for this I've seen for a long time

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

    dude, these are practices that aren't really spoken about, new programmers are just expected just to do them without any real explanation. Awesome content man

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

    Very well constructed and delivered! Bravo!

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

    Wow, this was explained very well.

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

    That was the best explanation for __name__ I have ever heard.

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

    There seem to be some variants of this, what is the difference between the following? if __name__ == '__main__': sys.exit(main()); if __name__ == '__main__': exit(main()); if __name__ == '__main__': main()

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

    Very nice! :-) For beginners, this should be a video to watch as mandatory. Can you extend this and have a best practices, if you want to have a bigger project of, for example, 2-3 files? One with functions, one main. And how to integrate "logging" into them or loading "config" from a xxx.conf file, for example with ConfigParser or ConfigObj?

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

      Agreed.
      It seems to me funny, though, to call this four best practices. It is only one, and suggestions 2, 3 and 4 are simply how to actually do #1, aren't they?

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

    Thank you, you helped me a lot. It was simple, straightforward, concise.

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

    Concise, clear, Commendable!!

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

    Your explanation was very useful for me. Thank u very much!!

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

    Under rated channel

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

    wow the quality of this video is really good. but i have to watch this video a couple of times to fully understand everything.

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

    Great video for python newcomers.

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

    Divide and conquer.
    Nice video!

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

    great leasson... pretty balance between deep and short material :) I suggest videos on good practices in different software architectures, as well as an analysis of pros and cons on which to choose given a real case ...

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

    This is exactly what I was looking for. Thank you !

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

    Thank you. Very helpful.

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

    Have you done this a different way ?
    def number_of_countries():
    def main():
    print(number_of_countries)
    if__name__== “__main__
    main()
    This is how the teacher wants our lab setup for tuples

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

    If you have no specific plan to use a part of the functionality by importing the file as a module, is this practice really useful? The length of the script inflated a lot. If you know that you're going to use the functionality, why don't you separate the reusable part into a module and write a small script main.py which imports the module and calls the functions?

    • @re.liable
      @re.liable 3 ปีที่แล้ว

      This is what I've been doing as well. I use the __name__ block in the modules as a sort of quick unit test

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

      That's what I do too

  • @АлександрНикитин-э5ы8э
    @АлександрНикитин-э5ы8э 2 ปีที่แล้ว

    What is this text editor?

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

    This is neat and tidy. Good work

  • @GauravSingh-de1pz
    @GauravSingh-de1pz 4 ปีที่แล้ว

    I have used without if __name==__main()
    from time import sleep
    print("This is my file to demostrate")
    def process_data(data):
    print("Beginn of data process")
    modified_data=data + "that has been modified"
    sleep(3)
    print("data processing finished")
    return modified_data
    data="My data read from web"
    process_data(data)
    modified_data=process_data(data)
    print(modified_data)
    What is the difference, if I do not use __name==__main() and did as above?
    COuld you please explain

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

      "data="My data read from web"
      process_data(data)
      modified_data=process_data(data)
      print(modified_data)"
      this code is outside of a function so it is ran automatically.

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

    This just blew my mind.

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

    Does everything you showed here work the same in python 3?

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

      yes I think they were using python 3 already lol

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

    You did not show the example of the first rule: put into the class

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

    I have the following questions (FYI: I run pycharm with Python 3 on Windows 10):
    the 'python3' command did not work in the pycharm terminal. Instead I had to use 'py'
    when I use the 'import' statement, I get the following error:
    'import' is not recognized as an internal or external command,
    operable program or batch file.
    when I add and run:
    if __name__ == "__main__":
    main()
    I get:
    Traceback (most recent call last):
    File "C:\Users\*\PycharmProjects\Exercises\best_practices.py", line 20, in
    main()
    NameError: name 'main' is not defined
    Could someone explain to me why all these functions do not work as in the video?
    Thanks in advance

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

    Thank you, very useful!

  • @hifromspace
    @hifromspace 4 ปีที่แล้ว

    How will this code look like with a lass statement?

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

    Great you are awesome!

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

    Thanks, bro!

  • @Andrew6James
    @Andrew6James 4 ปีที่แล้ว

    What is __name__ and '__main__'? They are not specified anywhere in the script so why are they evaluated as True or False? Can someone explain this please as I am very confused by this concept

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

    why do you have to import from the module? (from time import sleep) why cant you just simply import the module? (import time)

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

      When you import from the module (i.e. from time import sleep) you can just use sleep(number_of_seconds) in your code.
      If you import the whole module (i.e. import time), you have to use time.sleep(number_of_seconds).
      That's the only difference. 😊

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

    You miss one good practice point...
    You may have to name your main file "__main__. py" to be able to have a package to be run from named parent directory and that way no confusion about the first main file to run.

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

    This was so helpful!

  • @The4lexO
    @The4lexO 4 ปีที่แล้ว

    Doesn't work that way for a full POO project

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

    Thank you man.

  • @experimentalhypothesis1137
    @experimentalhypothesis1137 4 ปีที่แล้ว

    perfect explanation

  • @2112tux
    @2112tux 4 ปีที่แล้ว

    Nice Job. Thanks for doing.

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

    Remind me again, what are we creating in lines 13 and 14?

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

      We are creating a variable called data, assign a string to it and then print it. We then create a variable called data, assign a string to it and then print it.
      Repetition helps with learning I guess lol

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

    A glitch in the Matrix @ 4:35 & 4:43 ;)

  • @burakdev
    @burakdev 4 ปีที่แล้ว

    Awesome explanation m8

  • @ehtashamali821
    @ehtashamali821 4 ปีที่แล้ว

    Thanks

  • @MrTigerstyle80
    @MrTigerstyle80 4 ปีที่แล้ว

    Lines 13 and 14 were discussed twice. AI right?

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

    I wish my teachers had taught me Python this way..

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

      Hi can yu teach me Python from scratch

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

    Nice.

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

    Hi
    Can anyone teach me Python from scratch.

  • @Mr.Nichan
    @Mr.Nichan ปีที่แล้ว

    I clicked on this video just because I was entertained by the claim that Python even has "main functions".

  • @JC-el3zn
    @JC-el3zn 4 ปีที่แล้ว

    Spanish subtitles, plis :(

    • @chujcinaimie
      @chujcinaimie 4 ปีที่แล้ว

      English is must-have in programming :/

    • @nubcops220
      @nubcops220 4 ปีที่แล้ว

      Try learning english if you want to learn a programming language you can learn an english language easy as that

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

    making bot code for free fire game eaisly tell me more

  • @mrzero9859
    @mrzero9859 4 ปีที่แล้ว

    Can someone NOT stretch a video these days

  • @SAW1A1UFO
    @SAW1A1UFO 4 ปีที่แล้ว

    öc,åc