What does if name main do

0 votes

What does the if __name__ == "__main__": do?

# Threading example

import time, thread

def myfunction(string, sleeptime, lock, *args):

while True:

        lock.acquire()

        time.sleep(sleeptime)

lock.release()

time.sleep(sleeptime)

    if __name__ == "__main__":

        lock = thread.allocate_lock()

        thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))     

thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

Jul 30, 2018 in Python by bug_seeker
• 15,520 points
1,504 views

1 answer to this question.

0 votes

According to what I know, 

When the Python interpreter reads a source file, it executes all of the code found in it.

Before executing the code, it will define a few special variables. For example, 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.

In the case of your script, let's assume that it's executing as the main function, e.g. you said something like

python threading_example.py

on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that __name__ does equal "__main__", so it will execute the block shown there.

One reason for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.

answered Jul 30, 2018 by Priyaj
• 58,090 points

Related Questions In Python

0 votes
1 answer

what is if __name__ =='__main__: in python

Hello @Joy, '__main__' is the name of the scope ...READ MORE

answered Oct 20, 2020 in Python by Niroj
• 82,880 points
394 views
0 votes
1 answer

What exactly does the .join() method do?

Look carefully at your output: 5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5 ^ ...READ MORE

answered May 31, 2018 in Python by charlie_brown
• 7,720 points
390 views
0 votes
1 answer

What does the return statement do in Python?

The print() function is use to write ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 13,300 points
1,068 views
0 votes
1 answer

What exactly does the .join() method do?

Look carefully at your output: 5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5 ^                 ^                 ^ I've ...READ MORE

answered Oct 10, 2018 in Python by SDeb
• 13,300 points
347 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,411 views
0 votes
1 answer

What does eval() in Python do?

The eval function lets a Python program ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 58,090 points
527 views
+3 votes
2 answers

What does Python _init_ and self do?

Hey @Anirudh!  Self The self keyword is used to access ...READ MORE

answered Oct 31, 2018 in Python by Omkar
• 69,210 points
5,978 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP