[英]class is not defined despite being imported
我正在嘗試提高自己的python技能,並且嘗試編寫類,但是似乎遇到了一個非常令人困惑的錯誤。 盡管導入了包含我的類的.py文件,但python堅持認為該類實際上並不存在。
類定義:
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是定義的第一個函數仍然沒有用。...任何幫助將不勝感激。
使用標准名稱:
sayinghi = testclass.greeter("hello world!")
還有另一種import
形式,可以將greeter
帶入您的命名空間:
from testclass import greeter
import testclass
# change to
from testclass import greeter
要么
import testclass
sayinghi = greeter("hello world!")
# change to
import testclass
sayinghi = testclass.greeter("hello world!")
您導入了模塊/包,但需要引用其中的類。
您也可以這樣做
from testclass import *
但要提防命名空間污染
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.