智慧大石
我相信您想要的是这样的:来自对象的属性列表以我的拙见,内置功能dir()可以为您完成这项工作。取自help(dir)Python Shell上的输出:目录(...)dir([object]) -> list of strings如果不带参数调用,则返回当前作用域中的名称。否则,返回按字母顺序排列的名称列表,该列表包含给定对象的(某些)属性以及从中可以访问的属性。如果该对象提供了名为的方法__dir__,则将使用该方法;否则,将使用该方法。否则,将使用默认的dir()逻辑并返回:对于模块对象:模块的属性。对于一个类对象:其属性,以及递归其基类的属性。对于任何其他对象:其属性,其类的属性,以及递归地为其类的基类的属性。例如:$ pythonPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> a = "I am a string">>>>>> type(a)<class 'str'>>>>>>> dir(a)['__add__', '__class__', '__contains__', '__delattr__', '__doc__','__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__','__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__','__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__','__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__','__setattr__', '__sizeof__', '__str__', '__subclasshook__','_formatter_field_name_split', '_formatter_parser', 'capitalize','center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find','format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace','istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition','replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip','split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title','translate', 'upper', 'zfill']在检查您的问题时,我决定展示自己的思路,并以更好的格式输出dir()。dir_attributes.py(Python 2.7.6)#!/usr/bin/python""" Demonstrates the usage of dir(), with better output. """__author__ = "ivanleoncz"obj = "I am a string."count = 0print "\nObject Data: %s" % objprint "Object Type: %s\n" % type(obj)for method in dir(obj): # the comma at the end of the print, makes it printing # in the same line, 4 times (count) print "| {0: <20}".format(method), count += 1 if count == 4: count = 0 printdir_attributes.py(Python 3.4.3)#!/usr/bin/python3""" Demonstrates the usage of dir(), with better output. """__author__ = "ivanleoncz"obj = "I am a string."count = 0print("\nObject Data: ", obj)print("Object Type: ", type(obj),"\n")for method in dir(obj): # the end=" " at the end of the print statement, # makes it printing in the same line, 4 times (count) print("| {:20}".format(method), end=" ") count += 1 if count == 4: count = 0 print("")希望我有所贡献:)。