简体   繁体   English

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

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

For example, in this python module: https://github.com/bigcommerce/bigcommerce-api-python If I run this code: 例如,在以下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')

I get this traceback: 我得到此回溯:

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'

My question is, in this context, is it possible to get a list of the attributes of a given object? 我的问题是,在这种情况下,是否可以获取给定对象的属性列表? Or would I have to refer to the documentation to determine what attributes the Categories object would have? 还是我必须参考文档来确定Categories对象具有哪些属性?

This question is not specific to the bigcommerce python api, it's a general question about how to determine the attributes of a given object, I've just used the bigcommerce python api as an example. 这个问题不是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.

相关问题 如何从Python3模块中提取特定属性? - How can I pull specific attributes from a Python3 module? 如何获取没有__dict__的Python对象的属性列表? - How can I get a list of attributes of a Python object which doen't have __dict__? 如何在模块对象上访问dir()Python属性? - How to access dir() Python attributes on a module object? 如何在python pdb中列出对象的所有属性? - How do I list all the attributes of an object in python pdb? 如何在 Python 中模拟具有嵌套属性的对象? - How can I mock object with nested attributes in Python? 如何在python中创建用于设置对象属性的模板函数? - How can I create a template function for setting object attributes in python? 如何在 Python 的对象列表中获取单个项目的属性? - How can I get attributes of induvidual items in a list of Objects in Python? 如何在python列表中获取对象的选定属性? - How to get selected attributes of an object in to a python list? 如何使用列表同时从包/模块中调用多个方法/属性 - How can I call multiple methods/attributes from a package/module simultaneously using a list 我怎样才能拥有不是模块属性的 PyTorch 模块的子模块 - How can I have submodules of a PyTorch Module that are not attributes of the module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM