使用python的第三方库时,常常不清楚类包含了哪些成员和方法,下面介绍一种查看方法。
示例
以time为例。
import time
for name in dir(time) :
print(getattr(time, name))
输出
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']
11
This module provides various functions to manipulate time values.
There are two standard representations of time. One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).
The other representation is a tuple of 9 integers giving local time.
The tuple items are:
year (including century, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
<class '_frozen_importlib.BuiltinImporter'>
time
ModuleSpec(name='time', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in')
-32400
<built-in function asctime>
<built-in function ctime>
0
<built-in function get_clock_info>
<built-in function gmtime>
<built-in function localtime>
<built-in function mktime>
<built-in function monotonic>
<built-in function monotonic_ns>
<built-in function perf_counter>
<built-in function perf_counter_ns>
<built-in function process_time>
<built-in function process_time_ns>
<built-in function sleep>
<built-in function strftime>
<built-in function strptime>
<class 'time.struct_time'>
<built-in function thread_time>
<built-in function thread_time_ns>
<built-in function time>
<built-in function time_ns>
-28800
('中国标准时间', '中国夏令时')
说明
以上示例中,使用了dir和getattr这两个python的内置函数,它们的作用如下:
dir(obj):获取对象中所有的成员名称。
getattr(obj, name):获取对象的某个成员。