Python查看对象属性的方法

帮助https://docs.python.org/2/library/functions.html

dir() 函数

D:\pythontest>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> res = requests.get('https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2
nuyEZQRtN0U1Xf4Goe7gAE91qpuJw..')
>>> 
>>> 
>>> dir(res)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__
doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribu
te__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '
__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__redu
ce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '_
_subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'appa
rent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encodin
g', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content'
, 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reaso
n', 'request', 'status_code', 'text', 'url']
>>> 
>>> 
>>> print(res.url)
https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2nuyEZQRtN0U1Xf4Goe7gAE91
qpuJw..
>>> print(res.text)
<meta content="always" name="referrer"><script>window.location.replace("http://w
ww.hujiang.com/ciku/register/")</script><noscript><META http-equiv="refresh" con
tent="0;URL='http://www.hujiang.com/ciku/register/'"></noscript>

 help() 函数

调用内置帮助系统

>>> help(res)
Help on Response in module requests.models object:

class Response(builtins.object)
 |  The :class:`Response <Response>` object, which contains a
 |  server's response to an HTTP request.
 |
 |  Methods defined here:
 |
 |  __bool__(self)
 |      Returns True if :attr:`status_code` is less than 400.
 |
 |      This attribute checks if the status code of the response is between
 |      400 and 600 to see if there was a client error or a server error. If
 |      the status code, is between 200 and 400, this will return True. This
 |      is **not** a check to see if the response code is ``200 OK``.
 |
 |  __enter__(self)
 |
 |  __exit__(self, *args)
 |
 |  __getstate__(self)
 |
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 | 
 | 
 | 
 | 
 | 
 |  
 | 
 | 
 | 

-- More  --

#按q退出

vars() 函数

vars([object]) 返回object对象的__dict__属性,其中object对象可以是模块,类,实例,或任何其他有__dict__属性的对象。所以,其与直接访问__dict__属性等价。示例如下(这里是反例,mser对象中没有__dict__属性):

>>> vars(res)
{'request': <PreparedRequest [GET]>, 'reason': 'OK', 'headers': {'Server': 'ngin
x', 'UUID': '6e3340cd-af97-40b4-bc43-f61aef2f27fc', 'Content-Type': 'text/html;
charset=GBK', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Expires':
'Sat, 24 Aug 2019 16:01:02 GMT', 'Content-Encoding': 'gzip', 'Date': 'Sat, 24 Au
g 2019 16:01:02 GMT', 'Set-Cookie': 'ABTEST=0|1566662462|v17; expires=Mon, 23-Se
p-19 16:01:02 GMT; path=/, IPLOC=CN4601; expires=Sun, 23-Aug-20 16:01:02 GMT; do
main=.sogou.com; path=/, SUID=E5FF1D6F2513910A000000005D615F3E; expires=Fri, 19-
Aug-2039 16:01:02 GMT; domain=.sogou.com; path=/, black_passportid=1; domain=.so
gou.com; path=/; expires=Thu, 01-Dec-1994 16:00:00 GMT', 'Transfer-Encoding': 'c
hunked', 'P3P': 'CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE CO
M NAV OTC NOI DSP COR", CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA
 PRE COM NAV OTC NOI DSP COR", CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT
DEM STA PRE COM NAV OTC NOI DSP COR"', 'Cache-Control': 'max-age=0'}, 'status_co
de': 200, '_content_consumed': True, 'history': [], 'elapsed': datetime.timedelt
a(0, 0, 669786), '_content': b'<meta content="always" name="referrer"><script>wi
ndow.location.replace("http://www.hujiang.com/ciku/register/")</script><noscript
><META http-equiv="refresh" content="0;URL=\'http://www.hujiang.com/ciku/registe
r/\'"></noscript>', 'cookies': <RequestsCookieJar[Cookie(version=0, name='IPLOC'
, value='CN4601', port=None, port_specified=False, domain='.sogou.com', domain_s
pecified=True, domain_initial_dot=True, path='/', path_specified=True, secure=Fa
lse, expires=1598198462, discard=False, comment=None, comment_url=None, rest={},
 rfc2109=False), Cookie(version=0, name='SUID', value='E5FF1D6F2513910A000000005
D615F3E', port=None, port_specified=False, domain='.sogou.com', domain_specified
=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, exp
ires=2197382462, discard=False, comment=None, comment_url=None, rest={}, rfc2109
=False), Cookie(version=0, name='ABTEST', value='0|1566662462|v17', port=None, p
ort_specified=False, domain='www.sogou.com', domain_specified=False, domain_init
ial_dot=False, path='/', path_specified=True, secure=False, expires=1569254462,
discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>, 'url':
 'https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2nuyEZQRtN0U1Xf4Goe7gAE
91qpuJw..', '_next': None, 'encoding': 'GBK', 'raw': <urllib3.response.HTTPRespo
nse object at 0x00000000034BE9B0>, 'connection': <requests.adapters.HTTPAdapter
object at 0x00000000034AA4A8>}
>>>

type() 函数

type(object)返回对象object的类型。

>>> type(res)
<class 'requests.models.Response'>

callable() 函数

callable(object):若object对象是可调用的,则返回True,否则返回False。注意,即使返回True也可能调用失败,但返回False调用一定失败。

>>> callable(res)
False

 

转载于:https://www.cnblogs.com/linyouyi/p/11406638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值