Beginning Programming with Python For Dummies
Book image
Explore Book Buy On Amazon
Python gives you several different ways to view package content. The method that most developers use is to work with the dir() function, which tells you about the attributes that the package provides.
  • __builtins__: Contains a listing of all the built-in attributes that are accessible from the package. Python adds these attributes automatically for you.
  • __cached__: Tells you the name and location of the cached file that is associated with the package. The location information (path) is relative to the current Python directory.
  • __doc__: Outputs help information for the package, 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 package. The location information (path) is relative to the current Python directory.
  • __initializing__: Determines whether the package 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 package is done loading before you import another package that depends on it.
  • __loader__: Outputs the loader information for this package. The loader is a piece of software that gets the package 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 package.
  • __package__: This attribute is used internally by the import system to make it easier to load and manage packages. 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(BPPD_11_Packages.SayHello) and press Enter. You see the entries shown here

Python packages Drill down as far as needed to understand the packages that you use in Python.

Some of these entries, such as __name__, also appeared in the package 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 attributes Try getting some help information about the attribute you want to know about.

Python isn't going to blow up if you try the attribute. Even if the Notebook does experience problems, you can always restart the kernel (or simply restart the environment as a whole). So, another way to check out a package is to simply try the attributes. For example, if you type BPPD_11_Packages.SayHello.__sizeof__() and press Enter, you see the size of the SayHello() function in bytes.

Python attributes Using the attributes will help you get a better feel for how they work.

Unlike many other programming languages, Python also makes the source code for its native language libraries available. For example, when you look into the \Python36\Lib directory, you see a listing of .py files that you can open in Notebook with no problem at all. Try uploading the library by using the Upload button on the Notebook dashboard. Make sure to click Upload next to the file after you’ve opened it; then click the resulting link, and you see the content shown below. Note that .py files open in a simpler editor and don't display cells as the notebook files do that you’ve been using throughout the book.

package code Python Directly viewing package code can help in understanding it.

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.

About This Article

This article is from the book:

About the book author:

John Paul Mueller is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at

This article can be found in the category: