Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

__name__== __main__ in python

# If the python interpreter is running that module (the source file)
# as the main program, it sets the special __name__ variable to have
# a value “__main__”. If this file is being imported from another 
# module, __name__ will be set to the module’s name.
if __name__=='__main__':
  # do something
Comment

What does if __name_=="_main__": do?

# Suppose this is foo.py.

print("before import")
import math

print("before functionA")
def functionA():
    print("Function A")

print("before functionB")
def functionB():
    print("Function B {}".format(math.sqrt(100)))

print("before __name__ guard")
if __name__ == '__main__':
    functionA()
    functionB()
print("after __name__ guard")
Comment

if __name__="__main__":

# Suppose this is foo.py.

print("before import")
import math

print("before function_a")
def function_a():
    print("Function A")

print("before function_b")
def function_b():
    print("Function B {}".format(math.sqrt(100)))

print("before __name__ guard")
if __name__ == '__main__':
    function_a()
    function_b()
print("after __name__ guard")
Comment

What does if __name_=="_main__": do?

basically,   (__name__ == '__main__') ==True

confirms file is NOT a/part of a module and may continue execution  
Comment

__name__== __main__ in python



# Suppose this is foo.py.

print("before import")
import math

print("before functionA")
def functionA():
    print("Function A")

print("before functionB")
def functionB():
    print("Function B {}".format(math.sqrt(100)))

print("before __name__ guard")
if __name__ == '__main__':
    functionA()
    functionB()
print("after __name__ guard")


Comment

__main__(python)

<class '__main__.CoolClass'>
Comment

Python __name__ == "__main__"

if __name__ == "__main__"

// Every Python module has it’s __name__ defined 
// and if this is ‘__main__’, 
// it implies that the module is being run 
// standalone by the user and we can do corresponding
// appropriate actions.

// If you import this script as a module 
// in another script,
// the __name__ is set to the name
// of the script/module.
// Python files can act as either reusable modules, 
// or as standalone programs.

// if __name__ == “main”: is used to execute some 
// code only if the file was run directly, 
// and not imported.
Comment

PREVIOUS NEXT
Code Example
Python :: how to pick a random english word from a list 
Python :: cprofile implementation 
Python :: my pygame window wont stay open 
Python :: print all of dataframe 
Python :: python write 
Python :: python range backward 
Python :: how to change icon in pygame 
Python :: nlargest hierarchy series pandas 
Python :: remove all zeros from list python 
Python :: pyperclip 
Python :: how to sort in greatest to least python 
Python :: django timezone india 
Python :: save a seaborn heatmap 
Python :: how to import matplotlib.pyplo in python 
Python :: python execute file 
Python :: django filter text first character upper case 
Python :: spyder 3.3.6 requires pyqtwebengine<5.13; python_version = "3", which is not installed. 
Python :: nan float python 
Python :: how to address a column in a 2d array python 
Python :: python run as service windows 
Python :: click link selenium python 
Python :: django static url 
Python :: pytorch use multiple gpu 
Python :: docs.python.org 
Python :: initialize array of natural numbers python 
Python :: how to copy one dictionary to another in python 
Python :: python code formatter vs code 
Python :: plt.suptitle position 
Python :: how to seperate words and number in a list 
Python :: FileExistsError: [Errno 17] File exists: 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =