Python Forum
Strange the name of the file is changed automaticaly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange the name of the file is changed automaticaly
#1
Hi all! I find very strange that the name of the file is changed automatically. What becomes the first name I gave to my file: about_main.py
I think buran is very well placed to answer this.
#about_main.py
def funct():
    print("Value of __name__ is: ", __name__)
    print('This statement is in main function')
 
def someFn():
    print ('This function is not called under the main function')
	
someFn()
 
if __name__ == "__main__":
    funct()
	
	
"""                                               
Output                                                          
This function is not called under the main functon
Value of __name__ is:  __main__                    
This statement is in main function
******************************************************* 
People use __name__ to determine if the script is being imported or not. If it's equal to "__main__" then it's not an import. The idea being, you want to
run your main method if and only if you're not being imported.   .
***********
just to add to micseydel's answer. Given your sample code
if you execute your script it will execute both functions (because of line 9 and line 11-12)
if you import it in another script it will execute someFn (because of line 9) but will not execute the other one
*******************************
yes, you can just call them.
As to which is better - in my opinion it's better to get the habit of using if __name__ =='__main__': 
"""
  
  
  
  
  
  
Reply
#2
It's not the name of the file. It's a name of the scope.

https://docs.python.org/3/library/__main__.html

Quote:'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

foo.py
import bar
print(__name__)
bar.bar()
bar.py
def bar():
    print(__name__)
if you run foo.py the output would be
Output:
__main__ bar
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Can I consider that the first time the interpreter executes my file, it considers my file as __main__, and executes what is after the "if __name__ == "__main__" statement.
But if this file is imported by a second file, it no more executes what is under "if __name__ == "__main__" ???
Reply
#4
(Jun-05-2018, 06:37 PM)sylas Wrote: Can I consider that the first time the interpreter executes my file, it considers my file as __main__, and executes what is after the "if __name__ == "__main__" statement.

not only the first time, but every time when you execute your file

(Jun-05-2018, 06:37 PM)sylas Wrote: But if this file is imported by a second file, it no more executes what is under "if __name__ == "__main__"

yes, that is correct.
Actually read one more time my answer that you quote in your first post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange write()/File behavior kaega2 2 1,692 Jan-28-2022, 02:53 AM
Last Post: kaega2
  The behavior of tune model has changed Led_Zeppelin 5 4,458 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  how can a variable change if I haven't changed it? niminim 5 3,081 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 3,746 Jun-01-2019, 01:22 PM
Last Post: buran
  my list is being changed ivinjjunior 15 5,731 May-29-2019, 02:54 PM
Last Post: ivinjjunior
  RuntimeError: dictionary changed size during iteration anna 4 3,514 Feb-20-2019, 11:04 AM
Last Post: anna
  strange effect from duplicating a file descriptor Skaperen 1 2,069 Feb-18-2019, 08:20 PM
Last Post: Skaperen
  how to work with variables changed in definitions Prof_Jar_Jar 2 2,626 Dec-16-2018, 12:04 AM
Last Post: Prof_Jar_Jar
  RuntimeError: dictionary changed size during iteration Skaperen 1 9,152 Dec-10-2018, 10:14 PM
Last Post: nilamo
  Saving Values Changed in a database themick789 1 2,289 Nov-28-2018, 08:16 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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