尽管已导入,但未定义类 [英] class is not defined despite being imported
问题描述
我正在努力提高我的 Python 技能,我正在努力编写课程,但我似乎遇到了一个非常令人困惑的错误.尽管导入了包含我的类的 .py 文件,python 坚持认为该类实际上并不存在.
I'm trying to brush up on my python skills, and I'm dicking around with writing classes but I seem to have run into a really confusing error. Despite importing the .py file containing my class, python is insistent that the class doesn't actually exist.
类定义:
class greeter:
def __init__(self, arg1=None):
self.text = arg1
def sayHi(self):
return self.text
main.py:
#!/usr/bin/python
import testclass
sayinghi = greeter("hello world!")
print sayinghi.sayHi()
现在据我所知,我已经将所有文档都遵循到了t",我什至将参数初始化为 None,因为评估时间与创建时间限制等似乎对某些人来说是个问题,我已经确保 init 是第一个定义的函数,但仍然无济于事,尽管我有一个理论认为导入没有按预期工作......任何帮助将不胜感激.>
now as far as I can tell, I have followed all the documentation down to the 't', I even initialized arguments to None because of eval time vs creation time constraints etc which seemed to be a problem with some people, I have made sure init is the first function defined as well still to no avail, although I have a theory that the import is not working as it should.... Any help would be much appreciated.
推荐答案
使用完全限定名称:
sayinghi = testclass.greeter("hello world!")
import
的另一种形式可以将 greeter
带入您的命名空间:
There is an alternative form of import
that would bring greeter
into your namespace:
from testclass import greeter
这篇关于尽管已导入,但未定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!