繁体   English   中英

如何在python模块中列出对象的属性?

[英]How can I list the attributes of an object in a python module?

例如,在以下python模块中: https : //github.com/bigcommerce/bigcommerce-api-python如果我运行以下代码:

#!/usr/bin/env python2
import bigcommerce
import bigcommerce.api

# Bigcommerce credentials and path
BIG_URL = 'store-45eg5.mybigcommerce.com'
BIG_USER = 'henry'
BIG_KEY = 'api_key'

# api object definition
api = bigcommerce.api.BigcommerceApi(host=BIG_URL, basic_auth=(BIG_USER, BIG_KEY))

def create_category(name):
    rp = api.Categories.create(name=name)
    #if rp.status_code == 201:
    print(rp.status_code)
create_category('anothernewtestingcat12345')

我得到此回溯:

Traceback (most recent call last):
  File "./littletest.py", line 17, in <module>
    create_category('anothernewtestingcat12345')
  File "./littletest.py", line 16, in create_category
    print(rp.status_code)
AttributeError: 'Categories' object has no attribute 'status_code'

我的问题是,在这种情况下,是否可以获取给定对象的属性列表? 还是我必须参考文档来确定Categories对象具有哪些属性?

这个问题不是bigcommerce python api特有的,它是有关如何确定给定对象的属性的一般性问题,我只是以bigcommerce python api为例。

>>> class Simple:
...   def fun(self):
...     pass
...
>>> dir(Simple)
['__doc__', '__module__', 'fun']
>>> s = Simple()
>>> dir(s)
['__doc__', '__module__', 'fun']
>>> hasattr(s, 'fun')
True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM