當我的代碼全部在一個文件中時,它工作正常。現在,我將課程分成不同的模塊。這些模塊被賦予與類相同的名稱。也許這是一個問題,因爲MainPage
在加載時失敗。它是否認爲我試圖從模塊繼承?模塊/類名稱空間衝突是否可以發生?Python:Classname與文件/模塊名稱相同導致繼承問題?
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 ...
失敗回溯:
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)
更新我似乎解決了它。這個導入效果好多了:
from BaseHandler import BaseHandler
讓模塊和類名相同是不好的樣式嗎?
只要套管截然不同,只要模塊和類名相同,這種風格並不差。在Python中,約定模塊名稱全部爲小寫,類名稱爲CamelCase。我建議你閱讀Python風格指南(PEP 8) - http://www.python.org/dev/peps/pep-0008/。這非常有幫助! – jathanism 2010-03-06 18:29:56