我有一個包含兩個類的檔案 (PatchFinder.py):
class PatchFinder():
def __init__(self, dataset, pf: float):
#super(TopFeatures, self).__init__()
self.pf = pf
self.dataset = dataset
self.datas = dataset[0].to('cpu')
def TopFeaturesFind(self, g: Graph) -> Graph:
Zxx, self.edge_index_, edge_weights = g.unfold()
print(self.pf)
print(Zxx)
print(self.edge_index_)
print(edge_weights)
return Zxx
def anotherMethod(self):
print("something")
二級
class Class2(torch.nn.Module):
def __init__(self, dataset, hidden_channels):
super(GCN, self).__init__()
def forward(self, x, edge_index_):
return x
現在在另一個 File2.py 中我想獲取這些TopFeaturesFind()值。這就是我正在做的:
import PatchFinder
def main():
tf = PatchFinder()
t=tf.TopFeaturesFind()
print(t)
if __name__ == '__main__':
main()
但我收到錯誤:
tf = PatchFinder()
TypeError: 'module' object is not callable
我在這里做錯了什么?我來自java背景。這對我來說似乎沒問題。據我讀
如前所述:除了從物件內部無法訪問的“私有”實體變數在 Python 中不存在。來源
注意:我不想繼承PatchFinder,兩個檔案都在同一個檔案夾中。
uj5u.com熱心網友回復:
您import PatchFinder匯入了模塊,因此當您呼叫時,PatchFinder()您正在嘗試“初始化”模塊,而不是類。這里有指導:https : //docs.python.org/3/tutorial/modules.html
您需要做的就是指定要初始化模塊中定義的物件。更改tf = PatchFinder()為tf = PatchFinder.PatchFinder()它應該可以作業。
uj5u.com熱心網友回復:
PatchFinder 是您正在呼叫的模塊。訪問 PatchFinder 類使用
tf = PatchFinder.PatchFinder()
或者像這樣匯入類
from PatchFinder import PatchFinder
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381353.html
下一篇:使用全域臨時表的真實場景
