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

if name == main

# The code in this 'if' statement runs only if current file is the
# the main file. Mean this 'if statement' code will not run if you import
# that file into other program.

if __name__ == '__main__':
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

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

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

confirms file is NOT a/part of a module and may continue execution  
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

__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

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 :: python sqrt function 
Python :: python call function in class 
Python :: how to initialize set in python 
Python :: lists to dictionary python 
Python :: np.exp in python numpy 
Python :: how to change key to value and versa in python dictionary 
Python :: matplot lib 3d plot autoscale 
Python :: python program to find the sum of fibonacci series 
Python :: change base python 
Python :: upload file to aws 
Python :: python bytes to string 
Python :: tqdm command that works both in notebook and lab 
Python :: sns histplot nan values 
Python :: download images off unsplash python 
Python :: django url with string parameter 
Python :: pandas dataframe any along row 
Python :: python built in functions 
Python :: digital differential analyzer 
Python :: how to read .xlsx file in python 
Python :: activate virtual environment 
Python :: Python program to find N largest elements from a list 
Python :: how to remove role from people with a reaction discord bot python 
Python :: pyspark average group by 
Python :: color plt 
Python :: dash log scale 
Python :: how to get the length of a string in python 
Python :: how to convert string to datetime 
Python :: string list to int list python 
Python :: permutation of a string in python 
Python :: dict in dict in python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =