Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

PREVIOUS NEXT
Code Example
Python :: get request body flask 
Python :: how to get last n elements of a list in python 
Python :: python last 3 list elements 
Python :: print hexadecimal in python 
Python :: list square python 
Python :: how to merge two pandas dataframes on a column 
Python :: python numpy matrix to list 
Python :: round tuple 
Python :: how to write a code for anagram in python 
Python :: python get current class name 
Python :: run python file from another python file 
Python :: pandas count values by column 
Python :: python youtube downloader 
Python :: pass context data with templateview in django 
Python :: pandas isin 
Python :: how to capture cmd output in python 
Python :: python user input to tuple 
Python :: get user django 
Python :: how to take float input upto 2 decimal points in python 
Python :: enter selenium in python 
Python :: Read JSON files with automatic schema inference 
Python :: django get fields data from object model 
Python :: hashlib sha 256 
Python :: pandas replace non numeric values with 0? 
Python :: python rps 
Python :: python memory usage 
Python :: python declare variables from dictionary 
Python :: how to add csrf token in python requests 
Python :: matplotlib twinx legend 
Python :: null variable in python 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =