問題:我在調用程序中的類時遇到問題。Python:將類定義從一個.py導入到另一個問題時遇到問題
我創建了一個名爲程序,其example.py具有下面的代碼:
class ExampleBase:
"""
This is the ExampleBase class
"""
def __init__(self, company_name="N/A", stock_dict={}):
"""
class constructor
"""
self.company_name = company_name
self.stock_dict = stock_dict
return
def __str__(self):
"""
Prints the company name string
"""
str = "The Company name is: %s" %\
(self.company_name
)
return str
def add_purchase(self, addtlSTK):
"""
Adds item to stock_dict
"""
self.stock_dict.update(addtlSTK)
return
我試圖打電話給ExampleBase在其代碼是另一個程序:
import example
if __name__ == "__main__":
a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
b = example.ExampleBase("Bern", a)
有什麼問題和/或錯誤,你正在得到? – GWW
你是否正確縮進第一個文件?所有函數調用都應該從類中縮進例如: – smb564
可能的重複項:https://stackoverflow.com/a/4142178/3991696 –