我正在 Google App Engine 中试验简单的 Hello World 应用程序。我想创建一个单独的类,将其导入到 main.py 中并使用。
main.py:
import helloWorld
helloWorld.hi()
helloWorld.py:
class helloWorld():
def hi():
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
有什么解决方案可以让它发挥作用?
最佳答案
尝试这样的事情:
from helloWorld import helloWorld
helloWorld().hi()
或:
import helloWorld
helloWorld.helloWorld().hi()
第一个仅从模块helloWorld
导入类helloWorld
,您可以直接通过其名称使用它。
在第二个中,我们从模块 helloWorld
导入了所有内容,但我们只能使用 helloWorld.attr
语法来访问它。
并且 helloWorld
类的方法 hi
必须包含一个参数 self
。
def hi(self):
关于python - 类和 Google App Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16246014/