2010-06-01 50 views
5

我在Python中導入類沒有問題。我的工作流程是這樣的從另一個文件導入類和它的功能

index.py 

    class Template: 

     def header(): 
     def body(): 
     def form(): 
     def footer(): 

display.py 

我想調用函數header()body()footer()display.py頁。任何人都可以在python中明確這個問題。感謝你的關心。

索引文件--- [Index.py] [1]

[1]:http://pastebin.com/qNB53KTE和display.py - 「http://pastebin.com/vRsJumzq

+5

我建議你更多的Python教程,閱讀起來。那裏有很多例子。 '=]' – 2010-06-01 10:46:17

+1

另請注意,Python風格指南(http://www.python.org/dev/peps/pep-0008/)建議在類名中使用CapWords,而不是全部使用小寫字母。 – 2010-06-01 10:51:12

+1

感謝您的代碼!否則我們無法想出它。現在去接受更多的答案。 = 3 – 2010-06-01 11:38:01

回答

2

在索引文件的底部,創建一個HtmlTemplate對象並調用其上的所有方法。由於該代碼不包含在任何其他塊中,因此在導入模塊時會執行該代碼。您可能需要刪除它或檢查是否從命令行運行該文件。

if __name__ == "__main__": 
    objx=HtmlTemplate() 
    objx.Header() 
    objx.Body() 
    objx.Form() 
    objx.Footer() 
    objx.CloseHtml() 
1

我不知道如果我理解正確,但我相信你問的是如何在另一個腳本中導入template類。 import聲明是你需要的:

from index import template 

foo = template() 

foo.header() 
foo.body() 
foo.footer() 
+1

這裏有幾個錯別字,這使你的答案完全荒謬。 – SilentGhost 2010-06-01 10:42:50

+0

@Sil:我的答案的第一個修訂確實很麻煩。如果不是討厭的「你是人類」功能,在你的評論之前,錯別字會被修復:) – 2010-06-01 10:49:39

7

你有什麼試過?以下是導入後使用Template類的方法的正常方法。

from index import Template 

t = Template() 
t.header() 
t.body() 
t.footer() 

ETA:在您index.py文件(行99-105)你調用從上面的定義Template類中的所有功能結束。這就是你看到重複的原因。

+1

我已經嘗試過像你一樣。但我總是得到重複像2次身體()等 – MysticCodes 2010-06-01 10:52:03

+0

@用戶:你可能調用'模板''__init__'的方法。否則,此代碼中沒有任何內容會產生重複。 – SilentGhost 2010-06-01 10:54:00

+0

我給出了我的文件的鏈接謝謝 – MysticCodes 2010-06-01 11:29:51

1

編輯:好的,我看你的問題是什麼,給你的代碼。

你調用以下:

## Calling all the functions of the class template with object (objx) 
objx=HtmlTemplate() 
objx.Header() 
objx.Body() 
objx.Form() 
objx.Footer() 
objx.CloseHtml() 

,然後在display.py

t = HtmlTemplate() 
t.Header() 
t.Body() 

查看如何Body()被調用兩次?

作爲腳註,您應該使用方法名稱的小寫字母,以及現在正在使用的類的大寫單詞。這是一個很好的習慣。我極力推薦它。

您應該簡單地在display.py中構造一次對象並調用所有方法。

+0

是啊每個函數都有自我參數,我知道 – MysticCodes 2010-06-01 10:52:41

+0

你還有什麼其他問題?你能提供更多的代碼嗎? – 2010-06-01 10:56:55

+0

像我上面的工作流程,我有index.py文件[與給定的類和fucntions]和從index.py文件我想要從form()傳遞值到display.py。在display.py中,我只需要調用header(),body()和footer()。但我正在做同樣的事情,像你們說的,但在顯示頁面上重複。顯示頁面類似於這個header(),body(),form(),footer()和body()以及其他一些顯示頁面的cotent。 – MysticCodes 2010-06-01 11:03:14

1

你必須在頂部下面的代碼和index.py底部:

cgitb.enable() 
print 'Content-type: text/html\n\n' 
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" 

# [...] 

## Calling all the functions of the class template with object (objx) 
objx=HtmlTemplate() 
# [...]  
objx.CloseHtml() 

這將每次import index被調用。

爲了防止這種情況的發生,把它放在一個塊這樣的:

if __name__ == '__main__': 
    cgitb.enable() 
    print 'Content-type: text/html\n\n' 
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" 

    # [...] 

    ## Calling all the functions of the class template with object (objx) 
    objx=HtmlTemplate() 
    # [...]  
    objx.CloseHtml() 

...或更好把這個代碼的功能,可以從其他地方調用。

+0

Okei我上面提供的文件源代碼 – MysticCodes 2010-06-01 11:23:13

+0

提供的新答案! – Johnsyweb 2010-06-01 11:34:34

+0

它似乎工作,但仍然有一個錯誤,在display.py我收到頂部的「Content-type:text/html」 – MysticCodes 2010-06-01 11:38:01

1

以下解決方案爲我工作:

class1(unittest.TestCase): 
    def method1(self) 

class2(unittest.TestCase): 
    def method2(self): 
     instance_name = class1("method1") 
     instance_name.method1()