如何在另一个自定义过程中调用已定义过程?——Python



我不理解调用一个过程用于定义第二个过程的基本机制。我正在努力概念化一个简单的过程,调用另一个过程,试图自己弄清楚它,我已经在我的自学课程中达到了一个点,它将是有用的。

这方面的信息很难找到(也许我没有使用正确的关键字),我看到的唯一的例子太复杂了,我无法分解。如果你有我能读的网络文学作品,我会接受的。

如何定义一个过程并在其中首先调用过程?我知道{}用于字典,[]用于列表(或多或少),()用于字符串(或多或少)。

有我可以遵循的规则吗?我可以在任何地方调用第一个过程,比如在for循环(for e in first:)或if语句(if first:)中?在概念上有困难。我花了几个小时摆弄代码,试图弄清楚它,但运气不好。请帮我分析一下!

可以调用嵌套在if语句中的函数,也可以在循环中调用函数。没有人在编程时考虑这样的"规则"。在你摆弄代码之后,它将成为你的第二天性。

def print_hello_world(): # first function
    print "hello world"
def in_an_if_statement(): # a function that uses first func in if statement
    if 1 == 1:
        print_hello_world()
def in_a_loop(): # a function that uses first func in a loop
    for i in range(3):
        print_hello_world()
if __name__ == '__main__':
    in_an_if_statement()
    print '----'
    in_a_loop()

最新更新