2010-03-06 70 views
9

當我的代碼全部在一個文件中時,它工作正常。現在,我將課程分成不同的模塊。這些模塊被賦予與類相同的名稱。也許這是一個問題,因爲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 

讓模塊和類名相同是不好的樣式嗎?

+6

只要套管截然不同,只要模塊和類名相同,這種風格並不差。在Python中,約定模塊名稱全部爲小寫,類名稱爲CamelCase。我建議你閱讀Python風格指南(PEP 8) - http://www.python.org/dev/peps/pep-0008/。這非常有幫助! – jathanism 2010-03-06 18:29:56

回答

17

是的,模塊名稱與其他所有模塊名稱共享相同的名稱空間,並且,Python認爲您正嘗試從模塊繼承。

變化:

class MainPage(BaseHandler): 

到:

class MainPage(BaseHandler.BaseHandler): 

,你應該是好去。這樣,你就說「請從BaseHandler模​​塊中的BaseHandler類繼承」。

或者,你可以改變:

import BaseHandler 

到:

from BaseHandler import BaseHandler 
16

首先所有的文件名應該是全部小寫。這是Python的慣例,至少在大多數情況下有助於避免這種混淆。

接下來,您從MainHandler.py導入是錯誤的。您正在導入BaseHandler(該模塊),並將其引用爲類。這個班級實際上是BaseHandler.BaseHandler。你需要像這樣引用它。

試試看,它應該適合你。

+8

+1爲小寫的文件名提示 – 2011-11-16 07:18:09

相關問題