0

i find a way :

(1):the dir(object) is :

a="['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', '__weakref__', '_errors', '_fields', '_prefix', '_unbound_fields', 'confirm', 'data', 'email', 'errors', 'password', 'populate_obj', 'process', 'username', 'validate']"

(2):

b=eval(a)

(3)and it became a list of all method :

['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', '__weakref__', '_errors', '_fields', '_prefix', '_unbound_fields', 'confirm', 'data', 'email', 'errors', 'password', 'populate_obj', 'process', 'username', 'validate']

(3)then show the object's method,and all code is :

s=''
a=eval(str(dir(object)))
for i in a:
    s+=str(i)+':'+str(object[i])

print s

but it show error :

KeyError: '__class__'

so how to make my code running .

thanks

4

2 回答 2

2
s = ''.join('%s: %s' % (a, getattr(o, a)) for a in dir(o))
  • dir列出所有属性
  • for ... in创建一个返回每个属性名称的生成器
  • 检索对象的getattr属性值
  • %这些值插入到字符串中
  • ''.join所有字符串连接成一个
于 2010-06-05T14:00:40.107 回答
2
s += str(i)+':'+str(getattr(object, i))
于 2010-06-05T09:54:02.783 回答