python中__doc__、__name__、__file__的使用

各自的作用:

doc:获取到注释内容

name:获取到函数的名称

file:获取到当前的文件路径(获取模块路径)

示例代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from win32gui import *
import timeit
titles = set()
def foo(hwnd,mouse):
    """
    我是foo的注释
    """
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))

print("timeit.__doc__:",timeit.__doc__)
print("timeit.__name__:",timeit.__name__)
print("timeit.__file__:",timeit.__file__)

print("foo.__doc__:",foo.__doc__)
print("foo.__name__:",foo.__name__)
print("__file__:",__file__)

输出结果

timeit.__doc__: Tool for measuring execution time of small code snippets.

This module avoids a number of common traps for measuring execution
times.  See also Tim Peters' introduction to the Algorithms chapter in
the Python Cookbook, published by O'Reilly.

Library usage: see the
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章