2017-03-27 130 views
-2

我很新的python編程。我有兩個文本文件,其中一個是我的主程序,另一個是包含主文件中使用的一組函數和類的文件。我的問題是我如何使用函數文件中的函數和類,以及如何調用它們來正確運行主文件,以及我應該將該函數文件放在目錄中的位置? 這是我的函數文件中的示例:調用類和函數python

from numpy import * 
#################################################################### 
#################################################################### 
class C1: 

#################################################################### 
#################################################################### 
def F1: 

#################################################################### 
#################################################################### 
def F2: 

#################################################################### 
#################################################################### 
class C2: 
. 
. 
. 
+0

也許這將幫助你一點http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html – mic4ael

+0

閱讀[文件](HTTPS: //docs.python.org/2/tutorial/modules.html) –

回答

1

您可以簡單地將函數文件導入到main.py中。讓我們說它被稱爲funcs.py。然後,你可以說:

# near the top of the file 
from funcs import F1, F2, C1 
. 
. 
result = F1(p, q, r) + F2(p, q, r) 
c1 = C1() 
0

放置在同一個目錄這兩個文件,然後在你的主文件做:

import [name of function file] 

從此你可以做

foo = [name of function file].C2([your args here])