次のコードがあります。
#!/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 (最初の 2 行を切り替える) で実行すると、次のように失敗します。
<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)
どんな洞察も大歓迎です。
-ありがとう