2013-10-13 143 views
0

所以,我有1包的學生,一個班的學生,我有一個main.py那個包之外,我試圖創建學生例如創建另一個類的Python對象

class Student: 
    Id="" 

    def __init__(self, Id): 
     self.Id = Id 

獨立的對象文件main.py:

def main(): 
     print("is workign") 
     temp = Student("50") ## I want to create the object of class Student and send an attribute 

if __name__ == '__main__': 
    main() 

任何幫助將不勝感激。

+0

我希望你的縮進實際上並沒有那麼糟糕。 – bozdoz

+2

你需要在你的main.py中導入這個學生 – lucemia

回答

2

雖然在代碼之外定義類,但需要導入該類。

例如:

# in student.py 
class Student: 
    Id="" 

    def __init__(self, Id): 
     self.Id = Id 

# in main.py, assume main.py and student.py are in the same folder. 
def main(): 
    from student import Student 
    print("is workign") 
    temp = Student("50") ## I want to create the object of class Student and send an attribute 

if __name__ == '__main__': 
    main() 
+0

如果學生在一個子目錄下學習怎麼辦?因爲我有一個不同的包,所以student.py位於另一個名爲object – user2875416

+0

from subdic1.subdic2.student的文件夾中import Student – lucemia

相關問題