Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Hilton Garcia Fernandes
Hi, Ganesh !

Adding to what Christian already stated, i'd like to tell you that an
important use of this feature is the so-called modular testing, aka
unit testing. I mean: that way you can provide functionality in your
module to test it independently of any application it may be contained
in.

Unit testing in general is easier and quicker to do than to test the
whole application in which any given module is contained, along with
probably lots of other modules.

The Wikipedia article Unit Testing, at
https://secure.wikimedia.org/wikipedia/en/wiki/Unit_testing

will make things clear.

All the best,
hilton

On Fri, Maio 20, 2011, Christian Witts  said:

> On 2011/05/20 01:29 PM, Christian Witts wrote:
>> On 2011/05/20 01:09 PM, Ganesh Kumar wrote:
>>> Hi Gurus,
>>>
>>> I am new python programming.. I see many programs
>>> if __name__ == '__main__':
>>> when I check __name__ always eq __main__.
>>> what purpose use these structure.. please guide me..
>>>
>>> -Ganesh
>>>
>>
>> If you execute the script directly ie. python script.py the __name__
>> will be __main__ but if you import it it's the name of the file.
>>
>> #first.py
>> print __name__
>>
>> #second.py
>> import first
>>
>> $ python first.py
>> __main__
>>
>> $ python second.py
>> first
>>
> 
> Sorry, forgot to add before sending that the reason I use the `if 
> __name__ == '__main__'` structure is so that I can have a standalone 
> application that has it's defined entry point and then if I want to 
> reuse functions in the application I can import it without having to 
> worry that it will execute the entire thing.
> 
> -- 
> Christian Witts
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Hilton Garcia Fernandes
Nucleo de Tecnologias sem Fio (NTSF) -- Wireless Technologies Team
Lab de Sistemas Integraveis Tecnologico (LSI) -- Integrable Systems Lab
Escola Politecnica (Poli) -- Engineering School
Univ S Paulo (USP)
Tel: (5511)3091-5311 (work)
 (5511)8131-5213 (mobile)
Av. Prof. Luciano Gualberto,158 trav.3 CEP 05508-900
S. Paulo -- SP -- Brazil
Pagina inicial: http://www.lsi.usp.br/~hgfernan

Blog
http://bit.ly/8YITGc

com espelhamento em
http://bit.ly/4SIgzO




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Hilton Fernandes
Hi, Ganesh !

An important use of this feature is the so-called modular testing, aka
unit testing. I mean: that way you can provide functionality in your
module to test it independently of any application it may be contained
in.

Unit testing in general is easier and quicker to do than to test the
whole application in which any given module is contained, along with
probably lots of other modules.

The Wikipedia article Unit Testing, at
https://secure.wikimedia.org/wikipedia/en/wiki/Unit_testing

will make things clear.

All the best,
hilton

On Fri, May 20, 2011 at 8:31 AM, Christian Witts  wrote:
> On 2011/05/20 01:29 PM, Christian Witts wrote:
>>
>> On 2011/05/20 01:09 PM, Ganesh Kumar wrote:
>>>
>>> Hi Gurus,
>>>
>>> I am new python programming.. I see many programs
>>> if __name__ == '__main__':
>>> when I check __name__ always eq __main__.
>>> what purpose use these structure.. please guide me..
>>>
>>> -Ganesh
>>>
>>
>> If you execute the script directly ie. python script.py the __name__
>> will be __main__ but if you import it it's the name of the file.
>>
>> #first.py
>> print __name__
>>
>> #second.py
>> import first
>>
>> $ python first.py
>> __main__
>>
>> $ python second.py
>> first
>>
>
> Sorry, forgot to add before sending that the reason I use the `if __name__
> == '__main__'` structure is so that I can have a standalone application that
> has it's defined entry point and then if I want to reuse functions in the
> application I can import it without having to worry that it will execute the
> entire thing.
>
> --
> Christian Witts
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Christian Witts

On 2011/05/20 01:29 PM, Christian Witts wrote:

On 2011/05/20 01:09 PM, Ganesh Kumar wrote:

Hi Gurus,

I am new python programming.. I see many programs
if __name__ == '__main__':
when I check __name__ always eq __main__.
what purpose use these structure.. please guide me..

-Ganesh



If you execute the script directly ie. python script.py the __name__
will be __main__ but if you import it it's the name of the file.

#first.py
print __name__

#second.py
import first

$ python first.py
__main__

$ python second.py
first



Sorry, forgot to add before sending that the reason I use the `if 
__name__ == '__main__'` structure is so that I can have a standalone 
application that has it's defined entry point and then if I want to 
reuse functions in the application I can import it without having to 
worry that it will execute the entire thing.


--
Christian Witts
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Christian Witts

On 2011/05/20 01:09 PM, Ganesh Kumar wrote:

Hi Gurus,

I am new python programming.. I see many programs
if __name__ == '__main__':
  when I check __name__ always eq __main__.
what purpose use these structure.. please guide me..

-Ganesh



If you execute the script directly ie. python script.py the __name__ 
will be __main__ but if you import it it's the name of the file.


#first.py
print __name__

#second.py
import first

$ python first.py
__main__

$ python second.py
first

--
Christian Witts
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread spawgi
If the module is run as a program, then the __name__ is assigned the value
__main__ .
If the module is imported, then the value is not assigned.

Please see here -
http://stackoverflow.com/questions/419163/what-does-if-name-main-do


On Fri, May 20, 2011 at 4:39 PM, Ganesh Kumar  wrote:

> Hi Gurus,
>
> I am new python programming.. I see many programs
> if __name__ == '__main__':
>  when I check __name__ always eq __main__.
> what purpose use these structure.. please guide me..
>
> -Ganesh
>
> --
> Did I learn something today? If not, I wasted it.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Walter Prins
On 20 May 2011 12:09, Ganesh Kumar  wrote:

> Hi Gurus,
>
> I am new python programming.. I see many programs
> if __name__ == '__main__':
>  when I check __name__ always eq __main__.
> what purpose use these structure.. please guide me..
>
> -Ganesh


Here you go:

http://lmgtfy.com/?q=What+is+the+purpose+of+if+__name__+%3D+%27__main__%27

;)

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread Ganesh Kumar
Hi Gurus,

I am new python programming.. I see many programs
if __name__ == '__main__':
 when I check __name__ always eq __main__.
what purpose use these structure.. please guide me..

-Ganesh

-- 
Did I learn something today? If not, I wasted it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor