我有以下代码:
#!/usr/bin/env python
#!/usr/bin/env pypy
class my_components(object):
COMP0 = 0
COMP1 = 1
COMP1 = 2
__IGNORECOMP = -1
def attribute_dictionary(o):
d = { }
for attr in dir(o):
if attr[:2] != '__':
print o,attr
d[attr] = o.__getattribute__(o, attr)
return d
def main():
print attribute_dictionary(my_components)
if __name__ == '__main__':
main()
我用来在python中有效地做花哨的枚举。
它在 python 中效果很好,打印:
<class '__main__.my_components'> COMP0
<class '__main__.my_components'> COMP1
<class '__main__.my_components'> _my_components__IGNORECOMP
{'COMP0': 0, 'COMP1': 2, '_my_components__IGNORECOMP': -1}
但是,当我使用 pypy(切换前两行)运行它时,它会失败并显示:
<class '__main__.my_components'> COMP0
Traceback (most recent call last):
File "app_main.py", line 53, in run_toplevel
File "./pypy_Test.py", line 25, in <module>
main()
File "./pypy_Test.py", line 21, in main
print attribute_dictionary(my_components)
File "./pypy_Test.py", line 17, in attribute_dictionary
d[attr] = o.__getattribute__(o, attr)
TypeError: unbound method __getattribute__() must be called with my_components instance as first argument (got type instance instead)
任何见解都将受到欢迎。
-谢谢