Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python name = main

if __name__ == "__main__":
  # Code inside of here will only run if the python script was launched directly
  # This code will not run if imported as a module
Comment

__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

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

__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 :: python dataframe find no of true 
Python :: use chrome console in selenium 
Python :: round() function in python 
Python :: numpy find index of matching values 
Python :: python sort dictionary case insensitive 
Python :: python frozenset 
Python :: the requested resource was not found on this server. django 
Python :: python bigquery example 
Python :: Python NumPy column_stack Function Syntax 
Python :: how to create Varible in python 
Python :: Python NumPy tile Function Syntax 
Python :: first non repeating charcter in string ython 
Python :: google oauth python tutorial 
Python :: time complexity of data structures in python 
Python :: how to create a matrix from list in python 
Python :: pandas mask multiple condition 
Python :: how to print multiple integers in python in different line 
Python :: Python controller input 
Python :: python << meaning 
Python :: traduce query model 
Python :: Randome Word generator from consonant, vowel and specific string 
Python :: without @tf.function OOM 
Python :: from wireframe GUI design to python tkinter 
Shell :: uninstall libreoffice ubuntu 
Shell :: npm cache clean 
Shell :: git change username email 
Shell :: another git process seems to be running in this repository 
Shell :: nginx.service is not active, cannot reload. 
Shell :: run disk usage analyzer as root ubuntu 20.04 
Shell :: kde connect not showing devices 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =