Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Classname same as file/module name leads to inheritance issue?

My code worked fine when it was all in one file. Now, I'm splitting up classes into different modules. The modules have been given the same name as the classes. Perhaps this is a problem, because MainPage is failing when it is loaded. Does it think that I'm trying to inherit from a module? Can module/class namespace collisions happen?

MainPage.py

import BaseHandler
from models import Item
from Utils import render

class MainPage(BaseHandler):
    def body(self, CSIN=None): #@UnusedVariable
        self.header('Store')
        items = Item.all().order('name').fetch(10)
        render('Views/table.html', self, {'items': items})
        self.footer()

BaseHandler.py

from google.appengine.ext import webapp
from google.appengine.api import users
from Utils import *

# Controller
class BaseHandler(webapp.RequestHandler):
     # ... continues ... 

Failure traceback:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3180, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3123, in _Dispatch
    base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 515, in Dispatch
    base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2382, in Dispatch
    self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2292, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2188, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "C:\Users\odp\workspace\Store\src\Main.py", line 5, in <module>
    import MainPage
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1917, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1819, in FindAndLoadModule
    description)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1267, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1770, in LoadModuleRestricted
    description)
  File "C:\Users\odp\workspace\Store\src\MainPage.py", line 10, in <module>
    class MainPage(BaseHandler):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

It appears this can be solved by using

from BaseHandler import BaseHandler

Is it bad style to have the module and class name be the same?

like image 571
Nick Heiner Avatar asked Mar 06 '10 18:03

Nick Heiner


People also ask

Can a module and class have the same name in Python?

There are times when a module and a class in it have the same name. Then, in order to access the class name using method 1 (see p. 1), you must prefix it with the module name. If you do not specify a module name, an exception will be thrown.

Can two different packages have modules with same name?

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.

How do you inherit a module in Python?

Two built-in functions isinstance() and issubclass() are used to check inheritances. The function isinstance() returns True if the object is an instance of the class or other classes derived from it. Each and every class in Python inherits from the base class object .

How to inherit a class into another class in Python?

To create a class that inherits from another class, after the class name you'll put parentheses and then list any classes that your class inherits from. In a function definition, parentheses after the function name represent arguments that the function accepts.


2 Answers

Yes, module names share the same namespace as everything else, and, yes, Python thinks you are trying to inherit from a module.

Change:

class MainPage(BaseHandler):

to:

class MainPage(BaseHandler.BaseHandler):

and you should be good to go. That way, you're saying "please inherit from the BaseHandler class in the BaseHandler module".

Alternately, you could change:

import BaseHandler

to:

from BaseHandler import BaseHandler
like image 110
Daniel Stutzbach Avatar answered Oct 22 '22 14:10

Daniel Stutzbach


First of all the filenames should be all lowercase. That's Python convention that helps to avoid confusion such as this, at least most of the time.

Next, your import from withing MainHandler.py is wrong. You are importing BaseHandler (the module) and referencing it as if it were a class. The class is actually BaseHandler.BaseHandler. You need to reference it as such.

Try that and it should work for you.

like image 18
jathanism Avatar answered Oct 22 '22 14:10

jathanism