Skip to main content
Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

Script test.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py
python test.py

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call the above script from another script:

Script external_calling.py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this,

python external_calling.py
python external_calling.py

Output

42
I am inside hello_world
test
42
I am inside hello_world
test

So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.

Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

Script test.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call the above script from another script:

Script external_calling.py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this,

python external_calling.py

Output

42
I am inside hello_world
test

So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.

Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

Script test.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call the above script from another script:

Script external_calling.py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this,

python external_calling.py

Output

42
I am inside hello_world
test

So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.

Active reading [<https://en.wikipedia.org/wiki/Python_%28programming_language%29> <https://en.wiktionary.org/wiki/self-explanatory#Adjective> <https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s>]. Used more standard formatting.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Every module in pythonPython has aan attribute called __name__. The value of __name__ attribute is __main__ whenwhen the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

Script test.py

#Script test.py

apple = 42
 
def hello_world():
    print("I am inside hello_world")
 
if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py  

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call the above script from otheranother script:

Script external_calling.py

#script external_calling.py

import test 

print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this,

python external_calling.py

Output

42
I am inside hello_world
test

So, the above is self explanatory-explanatory that when you call testtest from otheranother script, if loop __name__ in test.py will not execute.

Every module in python has a attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

#Script test.py

apple = 42
 
def hello_world():
    print("I am inside hello_world")
 
if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py  

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call above script from other script

#script external_calling.py

import test
print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this

python external_calling.py

Output

42
I am inside hello_world
test

So, above is self explanatory that when you call test from other script, if loop __name__ in test.py will not execute.

Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

Script test.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call the above script from another script:

Script external_calling.py

import test 

print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this,

python external_calling.py

Output

42
I am inside hello_world
test

So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.

added 32 characters in body
Source Link
charlesreid1
  • 4.9k
  • 4
  • 35
  • 54

Every module in python has a attribute which is called as name __name__. The value of name__name__ attribute is 'main' __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of name__name__ is the name of the module.

Small example to explain in short.

#Script test.py

apple = 42
 
def hello_world():
    print("I am inside hello_world")
 
if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py  

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call above script from other script

#script external_calling.py

import test
print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this

python external_calling.py

Output

42
I am inside hello_world
test

So, above is self explanatory that when you call test from other script, if loop name__name__ in test.pytest.py will not execute.

Every module in python has a attribute which is called as name . The value of name attribute is 'main' when module run directly. Otherwise the value of name is the name of the module.

Small example to explain in short.

#Script test.py

apple = 42
 
def hello_world():
    print("I am inside hello_world")
 
if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py  

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call above script from other script

#script external_calling.py

import test
print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this

python external_calling.py

Output

42
I am inside hello_world
test

So, above is self explanatory that when you call test from other script, if loop name in test.py will not execute.

Every module in python has a attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.

Small example to explain in short.

#Script test.py

apple = 42
 
def hello_world():
    print("I am inside hello_world")
 
if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

We can execute this directly as

python test.py  

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

Now suppose we call above script from other script

#script external_calling.py

import test
print(test.apple)
test.hello_world()

print(test.__name__)

When you execute this

python external_calling.py

Output

42
I am inside hello_world
test

So, above is self explanatory that when you call test from other script, if loop __name__ in test.py will not execute.

Source Link
LOrD_ARaGOrN
  • 4.6k
  • 6
  • 39
  • 59
Loading