Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a Python module that contains class of same name work when imported?

I have recently been using web.py for some simple web applications in Python. I thought I understood how import statements, packages and modules worked but now I am a little confused.

According to web.py's API it says that the class application is located inside the module web.application. In order to use this class, the tutorial gives examples such as

import web
app = web.application(urls, globals())

What confuses me is how I am creating an instance of the application class using web.application. If there exists a class application that is inside a module named application within the web package, from what I have learned I would expect to have to do something as such :

web.application.application(urls, globals())

package -> module -> class

Could someone please clear up my confusion? Here is a link to the web.py API I am referencing http://webpy.org/docs/0.3/api#web.application

Thanks in advance for the help!

like image 540
bmcentee148 Avatar asked Jun 23 '16 16:06

bmcentee148


1 Answers

The attributes of application.py module have been imported into the __init__.py of the web package (via __all__).

In the __init__.py of the web package, you have:

from application import * # referring to application.py

And in application.py:

__all__ = [
    "application", "auto_application",
    "subdir_application", "subdomain_application", 
    "loadhook", "unloadhook",
    "autodelegate"
]

Therefore, the attributes specified in the __all__ found in application.py are now accessible directly from the top level import -- web.

You can read more about packages, __init__.py and __all__

like image 60
Moses Koledoye Avatar answered Sep 28 '22 18:09

Moses Koledoye