【问题标题】:What is the difference between a statement and a function in Python?Python中的语句和函数有什么区别?
【发布时间】:2017-04-16 09:39:29
【问题描述】:

编辑:建议的副本并没有回答我的问题,因为我主要关注的是 Python 的具体差异。建议的副本比这个问题要广泛得多。

我最近开始学习 Python。我目前正在阅读“Learn Python the Hard Way”。我有一些特别的编程经验,但这次我要从头开始学习一切。

在本书中,第一个课程涉及print,作者提供了在 Python 2.7 中使用它的各种说明,例如:

print "This is fun."

我发现自己想知道从编程的角度来看,这里在技术上叫什么 printSome research found this, PEP-3105

在这种情况下使print成为一个函数:

打印语句长期以来一直出现在可疑语言列表中 要在 Python 3000 中删除的功能,例如 Guido 的 “Python 遗憾”演示文稿1。因此,本 PEP 的目标 并不新鲜,尽管它在 Python 中可能会引起很多争议 开发人员。

所以print 在 Python 2.7 中是一个语句,在 Python 3 中是一个函数。

但我一直无法为statementfunction 之间的区别找到一个直截了当的定义。我发现this 也是由 Python 的发明者 Guido van Rossum 找到的,他在其中解释了为什么让 print 成为函数而不是语句会更好。

根据我的阅读,函数似乎是一些接受参数并返回值的代码。但是print 不是在 python 2.7 中这样做吗?它不是接收字符串并返回一个连接的字符串吗?

Python 中的语句和函数有什么区别?

【问题讨论】:

  • 仅供参考:sopython.com/wiki/LPTHW_Complaints。不,print 语句不返回任何内容(与 print 函数不同,它返回 None)。
  • 我确实看到了这个问题,但我主要关心的是这里的 Python,以及其他语言。在建议的副本中,选择的答案表明“在大多数语言中,语句不返回值”。 Python 中的语句和函数之间的区别不在那个答案中。
  • IMO 对我链接的问题的接受答案以一种与语言无关的方式很好地解释了它,这对 Python 也有效。第二个投票的答案是特定于 Python 的。如果您对其中任何一个都不满意,您应该阅读Python Language Reference,特别是关于复合语句的部分,您将在其中了解到function definitions 是语句的一个示例。
  • 感谢您的努力 mkrieger。我正在寻求了解函数和语句之间的区别。函数定义可能是语句的示例这一事实对我没有帮助,它有点循环。我试图了解函数具有哪些独特的品质,而 Python 中的语句没有,反之亦然,即 Python 中的语句和函数之间有什么区别。 Jim Fasarakis Hilliard 的回答在一定程度上有所帮助,如果有人愿意详细说明这将是有用的。

标签: python python-2.7 python-3.x function statements


【解决方案1】:

语句是一种语法结构。函数是一个对象。有创建函数的语句,比如def:

def Spam(): pass

So 语句是向 Python 表明您希望它创建函数的方法之一。除此之外,他们之间真的没有太大的关系。

【讨论】:

  • 你能详细说明这个答案吗? everything 不是语法结构吗?
【解决方案2】:

Python 中的语句是您编写的任何代码块。这更像是一个理论概念,而不是真实的东西。如果您在编写代码时使用正确的语法,您的语句将被执行(“评估”)。如果您使用不正确的语法,您的代码将引发错误。大多数人交替使用“陈述”和“表达”。

了解语句和函数之间区别的最简单方法可能是查看一些示例语句:

5 + 3 # This statement adds two numbers and returns the result
"hello " + "world" # This statement adds to strings and returns the result
my_var # This statement returns the value of a variable named my_var
first_name = "Kevin" # This statement assigns a value to a variable.
num_found += 1 # This statement increases the value of a variable called num_found
print("hello") # This is a statement that calls the print function
class User(BaseClass): # This statement begins a class definition
for player in players: # This statement begins a for-loop
def get_most_recent(language): # This statement begins a function definition
return total_count # This statement says that a function should return a value
import os # A statement that tells Python to look for and load a module named 'os'

# This statement calls a function but all arguments must also be valid expressions.
# In this case, one argument is a function that gets evaluated
mix_two_colors(get_my_favorite_color(), '#000000')

# The following statement spans multiple lines and creates a dictionary
my_profile = {
  'username': 'coolguy123' 
}

下面是一个无效的语句示例:

first+last = 'Billy Billson'
# Throws a Syntax error. Because the plus sign is not allowed to be part of a variable name.

在 Python 中,您倾向于将每个语句放在自己的行中,嵌套语句除外。但在 C 和 Java 等其他编程语言中,只要用冒号 (;) 分隔,您就可以将任意数量的语句放在一行中。

Python2和Python3都可以调用

print("this is a message") 

它会将字符串打印到标准输出。这是因为它们都定义了一个名为 print 的函数,该函数接受一个字符串参数并打印它。

Python2 还允许您在不调用函数的情况下将语句打印到标准输出。该语句的语法是它以 print 这个词开头,后面的就是打印的内容。在 Python3 中,这不再是一个有效的语句。

print "this is a message"

【讨论】:

  • 同样在 python 中,您可以在一行中放置任意数量的语句,只要它们用冒号 (;) 分隔即可。
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多