python学习[1]_python中的__name__属性和__doc__属性使用示

python中_name_属性和_doc_属性使用示例

1、_name_属性
用于判断当前模块是不是程序入口,如果当前程序正在使用,name的值为main

'''
display informaiton about a google calendar
'''
if __name__=='__main__':
    print __doc__
    #print globals()['__doc__']

输出结果:
display informaiton about a google calendar

display informaiton about a google calendar

2、_doc_属性

#使用DocStrings,每个对象都有自己的.__doc__属性
def printMax1(x,y):
    '''Prints the maximum of two numbers.the two values must be integers.'''
    print 
    x = int(x) #convert to integers,if possible.
    y = int(y)

    if x>y:
        print x,'is maximum.'
    else:
        print y,'is maximum.'
printMax1(3,5)
print printMax1.__doc__ #调用printMax1()函数中文档字符串属性

输出结果:

5 is maximum.
Prints the maximum of two numbers.the two values must be integers.

猜你喜欢

转载自blog.csdn.net/yuxiao_v/article/details/52942856
今日推荐