Python:从不同的def调用def

Python:从不同的def调用def

问题描述:

我对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

好吧,它很简单。你只是在你的其他函数中直接调用该函数。这里有一个例子:

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

所以你想调用相同的函数/方法十次?做一个循环呃?

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

如果我理解你的问题的权利,你只需要像这样使用它。

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

如果您问如何在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 

希望帮助。

所以你在做这样的事情?:

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