Python Forum
question about if __name__=="__main__"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about if __name__=="__main__"
#6
(Nov-13-2022, 04:17 PM)deanhystad Wrote: I'll wager on the error being a NameError caused by moving an assignment from global to function scope. Your program used to look like this:
def func():
    print(a)

if __name__ == "__main__":
    a = 5
    func()
And you changed it to this:
def func():
    print(a)

def main():
    a = 5
    func()

if __name__ == "__main__":
    main()
Error:
NameError: name 'a' is not defined
Am I close?

Actually yes, what ended up happening is my class variables became undefined, which threw a wrench in everything that worked previous to the swap. We ended up not needing to change anything in my script, but I was left with the question of why it didnt work.
Reply


Messages In This Thread
question about if __name__=="__main__" - by aaander - Nov-13-2022, 12:43 AM
RE: question about if __name__=="__main__" - by aaander - Nov-13-2022, 08:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  __name__ and __main__ in functions Mark17 3 880 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  if __name__=='__main__' albin2005 3 2,230 Sep-07-2021, 09:21 AM
Last Post: albin2005
  Questions about __name__ SheeppOSU 3 2,381 Jul-11-2019, 12:51 PM
Last Post: DeaD_EyE
  ModuleNotFoundError: No module named '__main__.vtt'; '__main__' is not a package MM2018 26 17,355 Oct-12-2018, 05:40 AM
Last Post: MM2018
  Why do we use __main__ when we can call a function directly? Dave7Swift 5 3,975 Jun-04-2018, 05:01 AM
Last Post: Dave7Swift
  Problem with __main__ sylas 2 2,903 Mar-30-2018, 07:51 PM
Last Post: snippsat
  [split] can't find '__main__' module Blue Dog 1 9,590 Oct-18-2016, 12:23 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020