Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python printing attributes with no __dict__

I am having trouble with scripting bridge for python

I am trying to list the attributes of the iTunes object

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

using

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

I get back

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

anyone know how to get around this?

like image 502
Ashley Hughes Avatar asked Jan 19 '13 04:01

Ashley Hughes


People also ask

What does __ dict __ mean in Python?

All objects in Python have an attribute __dict__, which is a dictionary object containing all attributes defined for that object itself. The mapping of attributes with its values is done to generate a dictionary.

What is __ Getattr __?

__getattr__(self, name) Is an object method that is called if the object's properties are not found. This method should return the property value or throw AttributeError . Note that if the object property can be found through the normal mechanism, it will not be called.

What is Python __ repr __?

Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.

What is __ module __ in Python?

A module in Python is a file (ending in .py ) that contains a set of definitions (variables and functions) that you can use when they are imported.


2 Answers

Try dir(iTunes). It's similar to vars, but more directly used with objects.

like image 182
Ned Batchelder Avatar answered Oct 08 '22 03:10

Ned Batchelder


for something similar to vars(obj), when obj is not accessible as a dict, I use a kludge like this:

>>> 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'}

I'm sure this could be improved upon, such as to filter out functions with 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}
like image 29
jcomeau_ictx Avatar answered Oct 08 '22 02:10

jcomeau_ictx