regarding main function

P

praba kar

Dear All,

I want to know the difference between calling
a function directly and through main function
compose()

if __name__ == __main__:
compose()



________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony
 
A

ajikoe

if you run in the same file there is no difference.

Block under "__main__" will be executed if you run that file only but
if you import that file from another file, then if you don't put your
code inside "__main__" your code it will be executed (this is not what
you want).

Example:

You have 1 file called mydll.py
inside mydll you have :

def func1():
print 'Helo world'

# you want to test your func1()
func1() # when you run you get 'Helo world'

then you have another file called MyCode.py which you like to call
func1(), inside this file you have this code:
import mydll
mydll.func1()

# you hope you get 'Helo World'
# In fact you get :
# 'Helo World'
# 'Helo World'
--------------
This happened because when you import mydll python will execute all
code in mydll including the one you put it for test.

So to overcome this problem if you want to test your code put inside
block __main__, next time you import your mydll it won't execute any
codes inside __main__

Hope this help.

Pujo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top