简体   繁体   English

无法理解这个python代码

[英]Unable to understand this python code

I was reading about python functions and saw this code: 我正在阅读python函数并看到这段代码:

def happyBirthday(person):
    print("Happy Birthday to you!")
    print("Happy Birthday to you!")
    print("Happy Birthday, dear " + person + ".")
    print("Happy Birthday to you!")

happyBirthday('Emily')
happyBirthday('Andre')

I couldn't understand why these brackets were being used for the print commands and so I removed them. 我无法理解为什么这些括号被用于打印命令,所以我删除了它们。

def happyBirthday(person):
    print "Happy Birthday to you!"
    print "Happy Birthday to you!"
    print "Happy Birthday, dear " + person + "."
    print "Happy Birthday to you!")

happyBirthday('Emily')
happyBirthday('Andre')

Even after removing those brackets I am getting the exact same results, so I am not sure which one is correct or whether I should use those brackets at all. 即使在删除这些括号后,我得到了完全相同的结果,所以我不确定哪一个是正确的,或者我是否应该使用这些括号。 Is it really necessary to use those brackets? 是否真的有必要使用这些括号?

One more thing. 还有一件事。 when I use the brackets then the +person+ gives the result as Happy Birthday, dear Andre. 当我使用括号时, +person+将结果作为生日快乐,亲爱的安德烈。 but when I use ,person, then it gives the result as <'Happy Birthday,dear ',' 'Andre','.'> 但当我使用时,person,然后它会给出结果为“生日快乐,亲爱的”,“安德烈”,“。”>

I am unable to understand these differences in the results. 我无法理解结果中的这些差异。 Could you shed some light on this? 你能否对此有所了解?

Is it really necessary to use those brackets? 是否真的有必要使用这些括号?

In Python 2.x, print is a statement, and the brackets are optional. 在Python 2.x中, print是一个语句,括号是可选的。

In Python 3.x, print() is a function, and the brackets are mandatory. 在Python 3.x中, print()是一个函数,括号是必需的。

It is considered good practice to use brackets even in Python 2.x, to ease eventual transition to Python 3.x. 即使在Python 2.x中使用括号也是一种很好的做法,以便于最终过渡到Python 3.x.

I am unable to understand these differences in the results. 我无法理解结果中的这些差异。 Could you shed some light on this? 你能否对此有所了解?

Here is what happens when you print several comma-separated things in Python 2.x: 以下是在Python 2.x中打印几个以逗号分隔的内容时发生的情况:

In [1]: print(1,2,3)
(1, 2, 3)

The above is interpreted as the print statement followed by a single argument, which is a tuple. 上面的解释是print语句后跟一个参数,它是一个元组。 The tuple is rendered with parentheses and commas. 元组用括号和逗号表示。

In [2]: print 1,2,3
1 2 3

The above is interpreted as the print statement followed by three arguments. 以上解释为print语句后跟三个参数。 Each argument is printed out separately, with spaces between them. 每个参数分别打印出来,它们之间有空格。

Neither version is great as far as compatibility with Python 3 is concerned: the first version is rendered differently, and the second is simply not valid Python 3 code. 就兼容Python 3而言,这两个版本都不是很好:第一个版本的呈现方式不同,第二个版本不是有效的Python 3代码。

With this in mind, I recommend that you stick with: 考虑到这一点,我建议你坚持:

print("Happy Birthday, dear " + person + ".")

This produces exactly the same results in both Python 2.x and Python 3.x. 这在Python 2.x和Python 3.x中产生完全相同的结果。

In Python2, print is a statement. 在Python2中,print是一个声明。 In Python3 it is a function 在Python3中它是一个函数

For some simple cases it works the same with the parentheses, but in general it is not as easy as just slapping parens around the print statement to port it to Python3 对于一些简单的情况,它与括号的工作方式相同,但一般来说,它并不像在print语句周围使用parens将其移植到Python3那么简单

eg. 例如。

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hi",
hi
>>> print("hi",)
('hi',)

vs VS

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hi",
  File "<stdin>", line 1
    print "hi",
             ^
SyntaxError: invalid syntax
>>> print("hi",)
hi

As already mentioned, print is a statement in Python 2 Then, when you do print('xxx'), the whole 如前所述,print是Python 2中的一个声明然后,当你打印('xxx')时,整个

('xxx')

is interpreted first. 首先解释。 Single parens are ignored, so this is simply a string which is printed. 单个parens被忽略,因此这只是一个打印的字符串。

When you do 'string' + name + 'string', ther strings are first concatenated (added), resulting in a string. 当你执行'string'+ name +'string'时,首先连接(添加)字符串,从而产生一个字符串。 It is then evaluated in parens, returning itself, and then printed. 然后在parens中进行评估,返回自身,然后打印。

On the opposite, when you do print('x', 'y', 'z'), the whole 相反,当你打印('x','y','z')时,整个

('x', 'y', 'z')

is a tuple , which is printed as <'Happy Birthday,dear ',' 'Andre','.'> 是一个元组 ,打印为“生日快乐,亲爱的”,“安德烈”,“。”>

在印刷品中尝试str(person)

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

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