{article Dive into Python}{title} {text}{/article}

dir function: dir returns a list of the attributes and methods of any
object: modules, functions, strings, lists, dictionaries... pretty much anything.

li = ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

dir(li)

d = {}

dir(d)

import odbchelper

dir(odbchelper)

>>> li = ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> dir(li) number
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> d = {}
>>> dir(d)number
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> import odbchelper
>>> dir(odbchelper)number
['__author__', '__builtins__', '__cached__', '__copyright__', '__date__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'buildConnectionString']
>>>

numberli is a list, so dir(li) returns a list of all the methods of a list. Note that the returned list contains the names of the methods as strings, not the methods themselves.

numberd is a dictionary, so dir(d) returns a list of the names of dictionary methods. At least one of these, keys, should look familiar.

This is where it really gets interesting. odbchelper is a module, so dir(odbchelper) returns a list of all kinds of stuff defined in the module, including built−in attributes, like __name__, __doc__, and whatever other attributes and methods you define. In this case, odbchelper has only one user−defined method, the buildConnectionString function described in Chapter 2.


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.

>>> import struct
>>> dir() # show the names in the module namespace
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'struct']
>>> dir(struct)
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_clearcache', 'calcsize', 'error', 'iter_unpack', 'pack', 'pack_into', 'unpack', 'unpack_from']
>>> class Shape:
def __dir__(self):
return ['area', 'perimeter', 'location']

>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
>>>