2013-01-19 68 views
6

我有使用Python的印刷沒有__dict__

>>> from pprint import pprint 
>>> from Foundation import * 
>>> from ScriptingBridge import * 
>>> iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes") 
>>> pprint (vars(iTunes)) 

我得到蟒蛇

我想列出iTunes的對象

iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes") 

的屬性與腳本橋麻煩屬性back

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: vars() argument must have __dict__ attribute 

任何人都知道如何解決這個問題?

回答

5

嘗試dir(iTunes)。它與vars類似,但更直接地用於對象。

+0

呀,工程 –

+0

@AshleyHughes它工作在這個意義上,不會拋出異常,但我實際上並沒有列出這些奇怪的包裝中可用的所有東西。例如,'dir(SBApplication)'中沒有'applicationWithBundleIdentifier_'。 – mmgp

+0

我只想看看什麼信息我可以看到IE瀏覽器是玩的東西,它不支持,但trackName返回None,所以我使用 –

1

這都已經很晚,但對於不同的問題(但同樣的錯誤),下面爲我工作:

json.dumps(your_variable) 

確保你在你的腳本在此之前進口的JSON。

import json 

您將需要找到一種以乾淨格式讀取JSON的方法。

+0

這不適用於許多對象,如Python文件對象。你會得到錯誤''>>> >>> json.dumps(f) Traceback(最近調用最後一個): 文件「」,第1行,在 TypeError:<打開文件'/tmp/test.tmp' ,0xf74722e0>處的模式'r'不是JSON serializable''' –

1

爲類似於瓦爾(OBJ),當obj是不是一個字典訪問的東西,我用這樣的雜牌組裝電腦:

>>> obj = open('/tmp/test.tmp') 
>>> print vars(obj) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: vars() argument must have __dict__ attribute 
>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_')) 

{'softspace': 0, 'encoding': None, 'flush': <built-in method flush of file object at 0xf7472b20>, 'readlines': <built-in method readlines of file object at 0xf7472b20>, 'xreadlines': <built-in method xreadlines of file object at 0xf7472b20>, 'close': <built-in method close of file object at 0xf7472b20>, 'seek': <built-in method seek of file object at 0xf7472b20>, 'newlines': None, 'errors': None, 'readinto': <built-in method readinto of file object at 0xf7472b20>, 'next': <method-wrapper 'next' of file object at 0xf7472b20>, 'write': <built-in method write of file object at 0xf7472b20>, 'closed': False, 'tell': <built-in method tell of file object at 0xf7472b20>, 'isatty': <built-in method isatty of file object at 0xf7472b20>, 'truncate': <built-in method truncate of file object at 0xf7472b20>, 'read': <built-in method read of file object at 0xf7472b20>, 'readline': <built-in method readline of file object at 0xf7472b20>, 'fileno': <built-in method fileno of file object at 0xf7472b20>, 'writelines': <built-in method writelines of file object at 0xf7472b20>, 'name': '/tmp/test.tmp', 'mode': 'r'}

我敢肯定,這可以改進如濾除功能與if not callable(getattr(obj, attr)

>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_') and not callable(getattr(obj, attr))) 
{'errors': None, 'name': '/tmp/test.tmp', 'encoding': None, 'softspace': 0, 'mode': 'r', 'closed': False, 'newlines': None}