2015-01-21 408 views
0

我對Python相對來說比較新,而且我一直被困在看起來很瑣碎的東西上,所以希望有人可以幫忙。Python:從不同的def調用def

我想要做的是調用Python中的方法內的方法。我想用多種不同的方法多次調用這個方法,所以我不想繼續複製和粘貼代碼,如果我要使用它~10次 - 只是爲了繼續調用「def 」。

我已經試過的東西,如:

return anotherMethod() 

任何想法如何做到這一點?

謝謝!

編輯: 對不起,問題的含糊性。我試圖讓我的頭腦在術語中出現。

def scaleC(): 

    pianoIcon = Icon("PianoScaleC.png") 
    d1 = Display("iPiano", pianoIcon.getWidth(), pianoIcon.getHeight()) 
    d1.add(pianoIcon) 
    return piano() 

def scaleCS(): 

    pianoIcon = Icon("PianoScaleCS.png") 
    d1 = Display("iPiano", pianoIcon.getWidth(), pianoIcon.getHeight()) 
    d1.add(pianoIcon) 
    return piano() 

def piano: 
#play the piano keys, etc 
+5

我想我們需要一些更多的上下文來理解你要去哪裏錯了,你可以編輯你的帖子,包括您更多的示例代碼? – Marius 2015-01-21 01:03:36

+1

你也許是指'self.anotherMethod()'? – 2015-01-21 01:03:58

+0

你不能在一個函數內「調用」一個函數。內部函數位於與環境不同的命名空間中,因此它不能直接訪問*(可以通過在包裝函數的返回語句中返回指向內部函數的指針來間接訪問它)。閱讀[裝飾](http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)以獲得更多的上下文。 – alfasin 2015-01-21 01:08:00

回答

-2

好吧,它很簡單。你只是在你的其他函數中直接調用該函數。這裏有一個例子:

def Examples(): 
    print 'Function successfully called!' 

def Example() 
    print 'Calling function...' 
    Examples() #This will call the function above it 

Example() 

你的結果應該是:

Calling function... 
Function successfully called! 

沒有錯誤應該彈出,這應該工作。只需添加一個變量:

loop = 0 

,並把功能在while循環:

while loop <> 10: 
    loop += 1 
    def Examples(): 
     print 'Function successfully called!' 

    def Example() 
     print 'Calling function...' 
     Examples() #This will call the function above it 

    Example() 

這將應該解決您的問題。我希望這可以幫助你!你的最終代碼與此暗示應該是:

loops = 0 
while loops <> 10: 
    loops += 1 
    def scaleC(): 
     pianoIcon = Icon("PianoScaleC.png") 
     d1 = Display("iPiano", pianoIcon.getWidth(), pianoIcon.getHeight()) 
     d1.add(pianoIcon) 
     piano() 

    def scaleCS(): 

     pianoIcon = Icon("PianoScaleCS.png") 
     d1 = Display("iPiano", pianoIcon.getWidth(), pianoIcon.getHeight()) 
     d1.add(pianoIcon) 
     piano() 

    def piano: 
     #play the piano keys, etc 
+2

爲什麼downvotes? – 2015-01-21 01:12:30

+0

這正是我正在尋找的!謝謝 - 你是一位救生員! – figub 2015-01-21 01:27:20

+0

非常歡迎您! – 2015-01-21 01:28:25

-1

所以你想調用相同的函數/方法十次?做一個循環呃?

for i in range(10): 
    print('Iteration Number %s' % i) 
    anotherMethod() 
如果你想十倍返回函數的結果列表中的

,您可以使用列表理解

return [anotherMethod() for _ in range(10)] 

注:_變量是一個用約定,當你需要有一個任務,但你不想存儲該變量,命名它_將有效地刪除它,據我所知。

我最後的想法是,你要引用可調用函數一次並調用它十次。你也可以這樣做(並且它是我最喜歡的一件關於python的東西)

my_method = anotherMethod 

my_method() 
my_method() 
... 
my_method() 
my_method() 
+0

我不相信這是所要問的。他們最初的意圖只是假設它可能被稱爲任意次數。 – 2015-01-21 01:09:32

-2

如果我理解你的問題的權利,你只需要像這樣使用它。

class YourClass(object): 
    def method(self): 
     pass # here goes your method 
    def method_where_i_want_to_use_other_method(self): 
     self.method() 
    #other things you need in this method 
+3

Err。你的意思是'def method_where_i_want_to_use_other_method(self):',當然? – 2015-01-21 01:10:41

+0

是的,在第二種方法之前,我的意思是確定。我可以在他的程序中添加那些功能不是空的。 – user3895596 2015-01-21 01:13:07

-1

如果您問如何在Python中定義可以隨處調用的方法,您可以在庫中編寫所需的方法。PY文件是這樣的:

def fun(a , b): 
    return a + b 

,然後從另一個文件調用它(即program.py。)這樣的:

from library import fun 

def main(): 
    # your code 
    print fun(1, 2) 
    a = 4 
    b = 5 
    c = fun(a, b) 
    print c 

if __name__ == '__main__': main() 

這將輸出:

3 
9 

希望幫助。

0

所以你在做這樣的事情?:

def my_global_function(): 
    def my_local_function(): 
     print('hello world') 

    my_local_function() 

然後外面要再次調用該函數的定義? 但是它已經被銷燬,因爲你退出了範圍。

你可以把它在全球則:

my_local_function_global_scope = None 

def my_global_function(): 
    def my_local_function(): 
     print('hello world') 

    # Declare variable space as global and assign to it 
    global my_local_function_global_scope 
    my_local_function_global_scope = my_local_function 

    my_local_function() 

my_local_function_global_scope() 
+1

你爲什麼回答兩次? – 2015-01-21 01:28:00

+0

@PythonMaster第一個已經downvotes不代表新建議的例子 – ThorSummoner 2015-01-21 01:34:14