python查看对象的所有成员

使用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):获取对象的某个成员。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值