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

你可能感兴趣的:(python那些事,python)