简体   繁体   English

何时调用/引用函数以及何时执行?

[英]When is a function called/referred to and when is it being executed?

I'm relatively new to Python and I have a (I guess) pretty basic question on functions in Python. 我对Python比较陌生,我对Python中的函数有一个(我猜)非常基本的问题。

I'm rewatching basics tutorials in order to really understand more of the structures and not just use them. 我正在重写基础教程,以便真正了解更多结构,而不仅仅是使用它们。 I used some basic code from a tutorial and tried different simple variations and I don't fully understand the outcomes and when a function is being referred to, ie when its return value is being called for, and when it's being executed. 我使用了教程中的一些基本代码并尝试了不同的简单变体,并且我不完全理解结果以及何时引用函数,即何时调用其返回值,以及何时执行它。

x=6

def example():

    globx = x
    print(globx)
    globx+=5
    print(globx)

example()

This defines the function and afterwards calls for it to be executed and as it's being executed it prints 6 and then prints 11, as expected. 这定义了函数,然后调用它来执行它,当它被执行时,它打印6然后按预期打印11。

Now: 现在:

x=6

def example():

    globx = x
    print(globx)
    globx+=5
    print(globx)

print(example())

I would have expected this to print "None" since print is looking for a return value of the function to print it but example() doesn't return a value. 我本来希望这打印“无”,因为print正在寻找函数的返回值来打印它,但是example()不返回值。 Instead 6, 11 and None are being printed. 而是打印6,11和无。 So I assume print(example()) calls for example()'s return value to print it but before also executes the function. 所以我假设print(example())调用example()的返回值来打印它,但在执行函数之前。 (Please correct me if I got that wrong.). (如果我弄错了,请纠正我。)

Even when I'm just assigning the return value to a variable x = example() after the definition of the function, it will also execute the function and print 6 and then 11. 即使我只是在定义函数后将返回值赋值给变量x = example(),它也会执行函数并打印6然后再打印11。

x=6

def example():

    globx = x
    print(globx)
    globx+=5
    print(globx)

x = example()

Is a function always being executed when it's written out? 函数在写出时是否总是被执行? (Ecxcept in the def) Is there a way to make use of a functions return value without it being fully executed? (在def中的Ecxcept)有没有办法在没有完全执行的情况下使用函数返回值? For example if I had a more complex code and at some point I want to make use of a functions return value but don't want it to be run. 例如,如果我有一个更复杂的代码,并且在某些时候我想使用函数返回值但不希望它运行。

Thanks in advance! 提前致谢!

You are getting confused by what a function returns and what a function does. 您对函数返回的内容以及函数的作用感到困惑。

In your case you have a function which has two print() statements. 在您的情况下,您有一个具有两个print()语句的函数。 Those statements have nothing to do with the value that the function will return and will print their corresponding values on every invocation of the function example() . 这些语句与函数返回的值无关,并将在函数example()每次调用时打印相应的值。

The return value of the function is defined using the return keyword and if it is not defined then it is None . 函数的返回值是使用return关键字定义的,如果未定义,则为None Obviously the function needs to be executed in order to get it to return a value. 显然,需要执行该函数以使其返回值。

What you say seems overall correct, even if it seems off what you expected. 你说的话似乎总体上是正确的,即使它看起来像你期望的那样。

Generally, you can see it as, when the function has parentheses at the end, ie example() , the function is executed. 通常,您可以将其视为,当函数末尾有括号时,即example() ,执行该函数。

Your last question is a bit vague, but you can stop executing the function at some point by using the return keyword inside the function. 您的上一个问题有点模糊,但您可以通过在函数内部使用return关键字在某个时刻停止执行该函数。 This makes sense in eg a function that performs some resource-intensive calculations, but occasionally there's a chance to take a shortcut. 这在例如执行一些资源密集型计算的函数中是有意义的,但偶尔也有机会采用快捷方式。

As an example 举个例子

def calculate_thing(shortcut = False):
    if shortcut:
        return 3
    # Resource-intensive, time-consuming calculations go here
    return result_of_calculations

Calling this function with calculate_thing(shortcut=True) will quickly return 3 , because the function stops executing when we hit return 3 . 使用calculate_thing(shortcut=True)调用此函数将快速返回3 ,因为当我们点击return 3时函数停止执行。 On the other hand, calling it by calculate_thing(shortcut=False) or calculate_thing() ( False is the default value for shortcut ) will make the function run for a while, doing some calculations, and then it returns whatever value was assigned to the variable result_of_calculations . 另一方面,通过calculate_thing(shortcut=False)calculate_thing()Falseshortcut的默认值calculate_thing()调用它将使函数运行一段时间,进行一些计算,然后返回分配给它的任何值。变量result_of_calculations

A function does something, it literally performs a function. 函数做某事,它实际上执行一个函数。 If you want that function to show you results as it's doing its job, you can print() things. 如果您希望该功能在其完成工作时向您显示结果,则可以打印()事物。 If you just want it to do its job and save the results for later, you return them to a variable that calls the function. 如果您只是希望它完成其工作并保存结果以供日后使用,则将它们返回到调用该函数的变量。 You can do both! 你可以做到这两点!

def just_print(input):
   print('Here is a function printing!', input)

just_print('cool!')
>> 'Here is a function printing!', 'cool!'


def return_value(input):
    return 'Hello ' + input

# We can store the return for future use
save_return_val = return_value('Ari')

print(save_return_val)
>> 'Hello Ari'

# Just print it
print(return_value('Ari'))
>> 'Hello Ari'

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

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