2013-04-26 173 views
1

我正在Google App Engine中试用简单的Hello World应用程序。我想创建一个单独的类,我将在main.py中导入并使用它。Classes和Google App Engine

main.py:

import helloWorld 

helloWorld.hi() 

helloWorld.py:

class helloWorld(): 
    def hi(): 
     print 'Content-Type: text/plain' 
     print '' 
     print 'Hello, world!' 

什么解决的办法得到这个工作?

回答

2

尝试这样:

from helloWorld import helloWorld 
helloWorld().hi() 

或:

import helloWorld 
helloWorld.helloWorld().hi() 

第一种方法只有从模块helloWorld导入类helloWorld,您可以直接通过其名称中使用它。

在第二个中,我们导入了模块helloWorld中的所有内容,但我们只能使用helloWorld.attr语法来访问它。

Docs on modules

helloWorld类的方法hi必须包含参数self

def hi(self): 
+0

我想这就是他其实是想:)虽然他的类方法签名是错误的:/ – 2013-04-26 22:17:24

+0

@JoranBeasley你说得对,我完全错过了。 – 2013-04-26 22:24:31

+0

谢谢......我认为我遇到的核心问题是yaml文件。出于某种原因,我不能让GAE正确地上传我的其他脚本,每当我尝试导入外部类时,都会给出500错误... – jumbopap 2013-04-26 22:33:39