python函数的基本解释 [英] Basic explanation of python functions

查看:108
本文介绍了python函数的基本解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有一个基本的问题来帮助我在Python中使用函数(在LPTHW教程中为uni做准备)。有人可以解释下面的语法,以及我的假设是否正确?

  def print_two_again(arg1,arg2):
printarg1:%r,arg2:%r% arg1,arg2)

print_two_again(Steve,Testing)

我明白 print_two_again 是该函数的名称,但具有 arg1 arg2 在旁边的括号里?它是在下面的打印命令中调用steve testing>吗?或者这些字符串指向 print 命令?

解决方案


使括号中的arg1,arg2接下来是
的目的是什么? 在这种情况下, arg1 arg2 称为参数。参数允许函数接收预期用于执行任务的输入。例如,在学校的数学中,你可能已经看到诸如 z = f(x, y)其中名为 f 的函数被定义为 f(x,y)= x + y 。这与编程语言中的概念是相同的。

它还允许您编写更通用,灵活和可重用的代码。例如,您不必编写许多不同版本的函数来完成相同的任务,但结果略有不同,从而避免了像 add2(x,y)= x + y add3(x,y,z)= x + y + z ,依此类推。你可以简单地做一些事情:

  def sum(values):#values是'list'类型的
结果= 0
值为值:
结果+ =值
返回结果

并且像这样调用它:

total = sum([1,2,3,4,5,6,7] )#带有数字的任何长度的列表



或者像这样:

total = sum([1,2])



函数需要多少个参数取决于它需要什么以及其他因素。

更新 $ b


什么让我困惑的是print_two_again(Steve,testing),这叫做
和它的目的是什么?

print_two_again(Steve,testing)是函数的调用(即函数调用)。这会导致程序跳转到名为 print_two_again 的函数的 body 并开始执行其中的代码。



(Steve,testing)部分是作为输入发送给函数的参数。这些是位置参数,它们基本上意味着它们被映射到名称 arg1 arg2

例如,考虑函数 f(x, y)= x - y 。如果这个函数被调用为 z = f(3,4),那么名称 x 的参数将接收值 3 y 将返回 4 ,以返回 -1 。如果您调用调用中的参数,那么您将有 x = 4 y = 3 d改为返回 1



这意味着函数调用中的参数顺序很重要



Python语言与许多其他语言一样,已经具有一组内置功能。名为 print 的函数就是一个例子。你可以使用 pydoc 命令获得很多信息(如果你使用Python3,那么我建议你使用 pydoc3 ) 。例如,命令 pydoc3 print 会生成以下文档:


-in函数print in module builtins:

print(...)
print(value,...,sep ='',end ='\\ \\ n',file = sys.stdout,flush = False)

 将值打印到流或sys.stdout默认。 
可选的关键字参数:
文件:类似文件的对象(流);默认为当前的sys.stdout。
sep:插入值之间的字符串,默认为空格。
end:在最后一个值后追加字符串,默认换行。
flush:是否强制刷新流。


请注意,这是Python3的文档。 Python2的文档将略有不同。

你的功能理解与你在学校的数学课程中看到的功能和在编程语言中看到的功能有直接的关系。这是因为数学是计算机科学和编程语言等基础学科的一部分(例如算法分析)。

I have a basic question below to help try get my head around functions in python (following the LPTHW tutorials in prep for uni). Could someone explain the syntax below, and whether I am correct with my assumptions?

def print_two_again(arg1, arg2):
    print "arg1: %r, arg2: %r" % (arg1, arg2)

print_two_again("Steve","Testing")

I understand that the print_two_again is the name of the function, but what is the purpose of having the arg1, arg2 in the parenthesis next to it? Is it to call the "steve" "testing" into the print command below? or do those strings go directing into the print command?

解决方案

what is the purpose of having the arg1, arg2 in the parenthesis next to it?

In this case, arg1 and arg2 are called arguments. Arguments allow functions to receive inputs it's expected to use in order to perform a task. The inputs are provided by the callers.

For example, in school math, you may've already seen things like z = f(x, y) where a function named f is defined as f(x, y) = x + y. This is the same concept in a programming language.

It also allows you do write more generic, flexible, and reusable code. For example, you don't have to write many different versions of a function to accomplish the same task with slightly different results, avoiding situations like add2(x, y) = x + y and add3(x, y, z) = x + y + z, and so on. You can simply do something like:

def sum(values):  # values is of type 'list'
    result = 0
    for value in values:
        result += value
    return result

And call it like this:

total = sum([1, 2, 3, 4, 5, 6, 7]) # a list of any length with numbers

Or like this:

total = sum([1, 2])

How many arguments a function needs will depend on what it needs to do and other factors.

Update

What confuses me is the print_two_again("Steve","testing") , what is this called and its purpose?

The line print_two_again("Steve","testing") is an invocation of the function (i.e. a function call). This causes the program to 'jump' into the body of the function named print_two_again and start executing the code in it.

The ("Steve","testing") part are the arguments being sent to the function as inputs. These are positional arguments, which basically means that they get "mapped" to the names arg1 and arg2 based on the order in which you've provided them when invoking the function.

For example, consider the function f(x, y) = x - y. If this function is called as z = f(3, 4) then the argument by the name of x will receive the value 3 and y will be 4, to return -1. If you reverse the arguments in the call, then you'd have x=4 and y=3 and it'd return 1 instead. The same is true of the arguments in the function you've provided.

This means that the order of the arguments in a function call is important.

The Python language, like many others, already has a set of built-in functionality. The function named print is an example of this. You can get a lot of information using the pydoc command (pydoc3 if you use Python3, which I'd recommend). For example, the command pydoc3 print produces the following documentation:

Help on built-in function print in module builtins:

print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

Note that this is documentation for Python3. Python2 documentation will be slightly different.

There's a direct correlation between your understanding of functions, as seen in your math courses in school, and functions as seen in a programming language. This is because math is part of the underlying foundation of computer science and programming languages, among others (e.g. analysis of algorithms).

这篇关于python函数的基本解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆