Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dir and help not showing all attributes of an object in python?

Tags:

python

This is my code so far,

import win32com.client as winc

outlook = winc.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
print message.body

and message.body prints my last e-mail from outlook.

When I dir(message) or help(message) the attribute body does not come. Why?

>>> dir(message)
['_ApplyTypes_', '_FlagAsMethod', '_LazyAddAttr_', '_NewEnum', '_Release_', '__AttrToID__', '__LazyMap__', '__call__', '__doc__', '__eq__', '__getattr__', '__getitem__', '__init__', '__int__', '__len__', '__module__', '__ne__', '__nonzero__', '__repr__', '__setattr__', '__setitem__', '__str__', '_builtMethods_', '_enum_', '_find_dispatch_type_', '_get_good_object_', '_get_good_single_object_', '_lazydata_', '_make_method_', '_mapCachedItems_', '_oleobj_', '_olerepr_', '_print_details_', '_proc_', '_unicode_to_string_', '_username_', '_wrap_dispatch_']

Why is this?

Update

Is there anyway I can get to know all attributes of an object without fail?

like image 577
ComputerFellow Avatar asked Oct 24 '13 11:10

ComputerFellow


Video Answer


3 Answers

Try to use __dict__ method. As it specified in docs, it lists all writable attributes, so that might help.

like image 37
Alexander Zhukov Avatar answered Nov 06 '22 12:11

Alexander Zhukov


As per this page http://docs.python.org/2/library/functions.html#dir

Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.

So, we should not rely on dir to get to know all about an object. If that class implements __dir__ method, we ll get only whatever is returned from that method. They might have implemented __dir__ and abstracted body from the dir calls.

Example:

class TestClass:
    def __init__(self):
        self.myValue = 0
    def myMethod(self):
        pass
    def __dir__(self):
        return []

class TestClass1:
    def __init__(self):
        self.myValue = 0
    def myMethod(self):
        pass

print dir(TestClass())
print dir(TestClass1())

print dir(TestClass())
print dir(TestClass1())

Output

[]
['__doc__', '__init__', '__module__', 'myMethod', 'myValue']
like image 136
thefourtheye Avatar answered Nov 06 '22 10:11

thefourtheye


Answer is NO, since as in this case, object's __getattr__ method can be overriden. Consider following example:

>>> class Const(object):
...     def __init__(self, val):
...         self.value = val
...     def __getattr__(self, a):
...         return self.value
...     def __setattr__(self, a, v)
...         self.__dict__[a] = v
...
>>> c = Const(1)
>>> dir(c)
['__class__', '__delattr__', '__dict__',  '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'value']
>>> c.__dict__
{'value': 1}
>>> c.some_strange_attribute_name
1
>>> c.some_strange_attribute_name = 2
>>> c.some_strange_attribute_name
2
like image 40
alko Avatar answered Nov 06 '22 10:11

alko