Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are some callable attributes not listed by the dir() function?

How come the dir() function in Python doesn't show all of the callable attributes?

import win32com.client

iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
currentTrack = win32com.client.CastTo(iTunes.CurrentTrack,"IITFileOrCDTrack")

print dir(currentTrack)

Result:

['AddArtworkFromFile', 'CLSID', 'Delete', 'GetITObjectIDs', 'Play', 'Reveal', 'UpdateInfoFromFile', 'UpdatePodcastFeed', '_ApplyTypes_', '__doc__', '__eq__', '__getattr__', '__init__', '__module__', '__ne__', '__repr__', '__setattr__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']

print currentTrack.Location

Location is callable and returns the file path, but is not listed in the first result. It also doesn't show up with code completion tools. Is it because it's being fetched through a getter method? I see it listed under _prop_map_get_ and _prop_map_put_.

Also, why does currentTrack.Location return a file path when currentTrack._prop_map_get_['Location'] returns "(1610874880, 2, (8, 0), (), 'Location', None)?" Where is it getting the file path string?

like image 976
tponthieux Avatar asked Nov 05 '10 17:11

tponthieux


1 Answers

In python, an object can have a __getattr__ method. It will be invoked for any attribute access for a non-existent attribute. It looks like this object is using _prop_map_get_ as part of its implementation of __getattr__.

Since __getattr__ can do arbitrary computation to satisfy the attribute request, and can raise AttributeError for names it can't handle, there's no way from the outside to list all attributes that are available.

like image 124
Ned Batchelder Avatar answered Oct 04 '22 18:10

Ned Batchelder