python 模块的__name__ __main__
每个模块都有一个名称,在模块中可以通过语句来找出模块的名称。这在一个场合特别有用
——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行。
每个Python模块都有它的__name__,如果它是'__main__',这说明这个模块被用户单独运行,
我们可以进行相应的恰当操作。
#!/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'
输出
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module
相关文档:
# -*- coding: cp936 -*-
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
att = MIMEText(open('e:\\test.txt').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/ ......
本篇将介绍python中sys, getopt模块处理命令行参数
如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?
需要模块:sys
参数个数:len(sys.argv)
脚本名: sys.argv[0]
参数1: sys.argv[1]
参数2: sys.argv[2]
test.py
1
import ......
Python太火了,不学点都感觉自己不是学计算机的,今天看了个不错的《简明python教程》,很不错。不过在学习过程中,居然发现了一个Python的bug,
#!/usr/bin/python
#coding=UTF-8
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
......
使用pdb调试Python程序
本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序
源码例子
例如,有模拟税收计算的程序:
#!/usr/bin/python
def debug_demo(val):
if val &l ......