Python的return如何理解?
比如learn python the hard way 里的
<span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">print</span> <span class="s">"ADDING </span><span class="si">%d</span><span class="s"> + </span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
回复内容:
请先看 @张晓峰 的答案。如果这个答案也看不懂——首先要了解,函数是什么?
书上可能会说函数是完成功能的模块之类的话。其实说白了,函数就是个你招来的工人。
你给他一些材料,告诉他怎么用这些材料拼装,然后他负责把拼装好的成品交给你。材料就是函数的参数,成品是函数的输出,而怎么拼装就是你写的函数体代码了。
比如这段代码
<span class="k">def</span> <span class="nf">worker</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span> <span class="n">x</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">c</span>
比如如果其他地方有一句
num = add(a, b)
无意间看到这个好2的问题..写一下我的粗浅见解..
1、函数(function)的功能是处理(process)参数(argument),从而得到一个返回值(return)
1.1、上一点也可以这么理解,函数是工序,参数是原材料,返回值是成品。
回答问题:
“这里的return a + b执行了什么操作?”
显然执行了a+b这个操作,并把a+b返回给这个叫add的函数,使a+b成为add函数的处理结果。
“return有什么含义,如何使用?”
函数的几个要素包括:函数名,参数,处理过程,返回结果(return),其中处理过程不必要,但其他三个是一定有的(但是return不一定要写,在定义函数时可以不写return,函数会默认返回None),对应你给的例子就是函数名add,参数a和b,返回结果a+b。return的含义你参考秋菊打官司:“俺就是要个说法(结果)嘛”或者参考函数的功能来理解。
至于如何使用,(以你的add函数为例)简单的总结有以下几点:
正常版:return a+b
手残版:returna+b #报错NameError
智障版:return a #只返回a
智障版2:return c+d #报错NameError
任性版:return 2 #就是这么2
任性版2:return a-b #你开心就好:)
空虚版:return #返回一个None print只是单纯的将结构输出至你的显示设备,再没有其它任何意义。
举个return的栗子:
def autoparts():
parts_dict={}
list_of_parts = open('list_of_parts.txt', 'r')
for line in list_of_parts:
k, v = line.split()
parts_dict[k] = v
return parts_dict
为什么return?如果你不return,那么你建立的这个字典就挂了,在function运行完后其结果不可再被访问,因为你仅仅是把结果print到输出设备上。如果你return了这个结果,那么结果会被保留,那么你还可以在这个结果的基础上做些其它事情,如:my_auto_parts=autoparts()
print my_auto_parts['engine']
当我们再调用autoparts这个function时,它将return的值给我们,我们可以把这个return的值储存在my_auto_parts这个变量里,我们可以在autoparts这个function运行完后,依然通过my_auto_parts这个变量来获取字典里的内容,最后我们可以输出字典里关键词“engine”的目标。
首先,你是在定义一个函数,使用它的时候要调用它sum=add(3,4)
输出
sum=7
可见,return把后面表达式的结果返回给了sum
重点是要明白 定义函数 然后 使用函数,这是两个步骤。 啊哈哈哈!我在知乎回答的第一道专业题!
返回你def的这个函数 用中文“返回”两字来解释这个概念的,都TM在说废话,人家问如何理解,你就把英文翻译过来有意义么

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
