[Tutor] '__name__' == '__main__'

Christian Witts cwitts at compuscan.co.za
Tue Feb 21 06:27:55 CET 2012


On 2012/02/21 06:55 AM, Michael Lewis wrote:
> I am back to being confused. I just tried running the module without 
> first importing it, and it worked just fine. How do I do this properly 
> to where the module only runs if I import it?
>
> Code:
>
> def MultiplyText(text, multiplier):
>     '''Recieve a S & int. For digits in S, multiply by multiplier and 
> return updated S.'''
>     return ' '.join(str(int(num) * multiplier) if num.isdigit() else 
> num for num in text)
>
>
> def GetUserInput():
>     '''Get S & multiplier. Test multiplier.isdigit(). Call 
> MultiplyText(text, multiplier)'''
>     text = raw_input('Enter some text: ')
>     while True:
>         multiplier = raw_input('Enter a multiplier: ')
>         try:
>             multiplier = int(multiplier)
>             break
>         except ValueError:
>             continue
>     return MultiplyText(text.split(), multiplier)
>
>
> if "__name__" == '__main__':
>     GetUserInput()
>
> What I did in IDLE:
>
> >>>
> >>> GetUserInput()
> Enter some text: 4 times
> Enter a multiplier: 2
> '8 times'
> >>>
>
> -- 
> Michael J. Lewis
> mjolewis at gmail.com <mailto:mjolewis at gmail.com>
> 415.815.7257
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

If you change '__main__' to the name of the file, without the extension, 
it will match __name__.
Alternatively, you can have an else after your `__name__ == '__main__'`

$ cat t1.py
if __name__ == '__main__':
     print 'Executed from __main__'

if __name__ == 't1':
     print 'Executed by import from t2'

$ cat t2.py
import t1

$ python t1.py
Executed from __main__

$ python t2.py
Executed by import from t2

$ cat t3.py
if __name__ == '__main__':
     print 'Executed from __main__'
else:
     print 'Executed by import'

$ cat t4.py
import t3

$ python t3.py
Executed from __main__

$ python t4.py
Executed by import

-- 

Christian Witts
Python Developer

COMPUSCAN | CONFIDENCE IN CREDIT

Telephone : +27 21 888 6000
National Call Centre : 0861 51 41 31
Fax : +27 21 413 2424
Email : cwitts at compuscan.co.za <mailto:cwitts at compuscan.co.za>
Website: www.compuscan.co.za <http://www.compuscan.co.za>

Would you like to hear more from us? Register here and receive our 
newsletters and other business communications. 
<http://compuscan.us2.list-manage.com/subscribe?u=b14cacd3a29021ca07d2b32b9&id=163a9926e2>

*/NOTE:/* This e-mail (including attachments) is subject to the 
disclaimer published at our website 
<http://www.compuscan.co.za/about-us/132-email-disclaimer>.
If you cannot access the disclaimer, request it from 
email.disclaimer at compuscan.co.za 
<mailto:email.disclaimer at compuscan.co.za> or 0861 514131.
National Credit Regulator Credit Bureau Registration No. NCRCB6
/Please consider the environment before printing/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120221/be2a3483/attachment.html>


More information about the Tutor mailing list