简体   繁体   English

如何在另一个 def 过程中调用已定义的过程? - Python

[英]How do I call a defined procedure within another def procedure? - Python

I don't understand the basic mechanics of calling a procedure for use in defining a second one.我不明白调用用于定义第二个过程的过程的基本机制。 I'm struggling to conceptualize a simple procedure calling another in trying to figure it out on my own and I've gotten to a point in my self-paced lessons where it would be useful.我正在努力将一个简单的程序概念化,以试图自己解决这个问题,并且我已经在我的自定进度课程中达到了有用的程度。

Information on this is hard to find (perhaps I'm not using the right keywords) and the only examples I've seen were way too complicated for me to break down.这方面的信息很难找到(也许我没有使用正确的关键字),而且我看到的唯一例子太复杂了,我无法分解。 If you have any internet literature I can read, I'll accept that.如果你有任何我可以阅读的网络文学,我会接受。

How can I def a procedure second and call procedure first within it?如何在第二个过程中定义一个过程并首先在其中调用过程? I know that {} are for dictionaries, [] for lists (more or less), and () for strings (more or less) at this point.我知道此时 {} 用于字典,[] 用于列表(或多或少),和 () 用于字符串(或多或少)。

Is there a rule I can follow?有我可以遵守的规则吗? Can I call the first procedure anywhere, like in a for loop (for e in first:) or an if statement (if first:)?我可以在任何地方调用第一个过程,比如在 for 循环(for e in first:)或 if 语句(if first:)中? Having trouble conceptualizing this one.很难概念化这个。 I've spent hours on this playing with the code, trying to figure it out with no luck.我花了几个小时来玩这个代码,试图在没有运气的情况下弄清楚。 Please help break it down for me!请帮我分解一下!

You can call a function nested in an if statement and you can call a function inside a loop.您可以调用嵌套在 if 语句中的函数,也可以调用循环内的函数。 Nobody thinks about "rules" like these when they program.没有人在编程时会考虑这样的“规则”。 After you play around with code, it'll become second nature.在你玩弄代码之后,它就会成为你的第二天性。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM