How to View Module Content in Python
Python gives you several different ways to view module content. The method that most developers use is to work with the dir() function, which tells you about the attributes that the module provides.
Function attributes are automatically generated by Python for you. These attributes perform the following tasks or contain the following information:
__builtins__: Contains a listing of all the built-in attributes that are accessible from the module. Python adds these attributes automatically for you.
__cached__: Tells you the name and location of the cached file that is associated with the module. The location information (path) is relative to the current Python directory.
__doc__: Outputs help information for the module, assuming that you’ve actually filled it in. For example, if you type os.__doc__ and press Enter, Python will output the help information associated with the os library.
__file__: Tells you the name and location of the module. The location information (path) is relative to the current Python directory.
__initializing__: Determines whether the module is in the process of initializing itself. Normally this attribute returns a value of False. This attribute is useful when you need to wait until one module is done loading before you import another module that depends on it.
__loader__: Outputs the loader information for this module. The loader is a piece of software that gets the module and puts it into memory so that Python can use it. This is one attribute you rarely (if ever) use.
__name__: Tells you just the name of the module.
__package__: This attribute is used internally by the import system to make it easier to load and manage modules. You don’t need to worry about this particular attribute.
It may surprise you to find that you can drill down even further into the attributes. Type dir(MyLibrary.SayHello) and press Enter.
Some of these entries, such as __name__, also appeared in the module listing. However, you might be curious about some of the other entries. For example, you might want to know what __sizeof__ is all about. One way to get additional information is to type help(“__sizeof__”) and press Enter. You see some scanty (but useful) help information.
Python isn’t going to blow up if you try the attribute. Even if the shell does experience problems, you can always start a new one. So, another way to check out a module is to simply try the attributes. For example, if you type MyLibrary.SayHello.__sizeof__( ) and press Enter, you see the size of the SayHello() function in bytes.
Unlike many other programming languages, Python also makes the source code for its native language libraries available. For example, when you look into the Python33Lib directory, you see a listing of .py files that you can open in IDLE with no problem at all.
Viewing the content directly can help you discover new programming techniques and better understand how the library works. The more time you spend working with Python, the better you’ll become at using it to build interesting applications.
Make sure that you just look at the library code and don’t accidentally change it. If you accidentally change the code, your applications can stop working. Worse yet, you can introduce subtle bugs into your application that will appear only on your system and nowhere else. Always exercise care when working with library code.