/*********************************************************************************************************
-- Title : [py] __name__ == '__main__'의 의미(namespace)
-- Reference : http://ibiblio.org/
-- Key word : 네임스페이스 namespace name space python 파이썬
*********************************************************************************************************/


Example 8.2. Using a module's __name__

				
#!/usr/bin/python
# Filename: using_name.py

if __name__ == '__main__':
	print 'This program is being run by itself'
else:
	print 'I am being imported from another module'
				
				


Output

				
$ python using_name.py
This program is being run by itself

$ python
>>> import using_name
I am being imported from another module
>>>
				
				


How It Works

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.


직접 실행하는 것이라면 True라서 수행되고, 호출되어 것(import)이라면 False라서 해당 if문은 수행이 안됨.

+ Recent posts