• 欢迎光临~

海象运算符

开发技术 开发技术 2022-10-16 次浏览

Python 3.8功能 (Python 3.8 Feature)

Walrus operator is the coolest feature that was added in the Python 3.8 update, the Python release that most of you are probably using right now. The operator := is called the walrus operator since it characterizes the look of the walrus. See the colon as eyes and the equal sign as its tusks.

Walrus运算符是Python 3.8更新中添加的最酷的功能,这是大多数人现在可能正在使用的Python版本。 :=运算符称为海象运算符,因为它表征了海象的外观。 将结肠视为眼睛,将等号视为其牙。

The walrus operator is used as an assignment expression. It allows you to assign a value to a variable while also returning the value.

海象运算符用作赋值表达式。 它允许您为变量分配一个值,同时还返回该值。

那么我们该如何使用呢? (So How Do We Use This?)

If you are wondering whether this operator will be useful or not, you should read this!

如果您想知道此运算符是否有用,则应阅读本文!

Say that you have a program that prompts the user to tell what is their favorite color is and you want the program to send back a message to the user that includes the answer. Conventionally, you will do this:

假设您有一个程序来提示用户说出他们最喜欢的颜色是什么,并且您希望该程序向用户发送包含答案的消息。 按照惯例,您将执行以下操作:

start = input("Do you want to start(y/n)?")print(start == "y")

But with walrus operator, we can make it more compact.

但是使用海象运算符,我们可以使其更紧凑。

print((start := input("Do you want to start(y/n)?")) == "y")

Both of the snippets will print the same result, printing True if the input is y and False otherwise. Do you recognize that I used double parentheses in the second snippet? In this snippet, even without double parentheses, the preferred result True/False will still be achieved. However, it should be noted that using parentheses with the walrus operator is encouraged. It can save you a lot of time.

这两个代码片段将打印相同的结果,如果输入为y,则打印True,否则为False。 您是否知道我在第二个片段中使用了双括号? 在此代码段中,即使没有双括号,仍将获得首选结果True / False。 但是,应该注意的是,鼓励在海象运算符中使用括号。 它可以节省您很多时间。

它为什么如此重要? (Why Is it Important?)

Compare these two snippets:

比较以下两个片段:

  1. With parentheses

     

    带括号
if (sum := 10 + 5) > 10:    print(sum) #return 15 

2. Without parentheses

2.无括号

if sum := 10 + 5 > 10:    print(sum) #return True

Observe that in the second example, the value True is assigned in the sum variable. This is because the part > 10 is included in the evaluation of the sum variable.

请注意,在第二个示例中,在sum变量中分配了值True。 这是因为部分> 10包含在求和变量的评估中。

最后的想法 (Final Thoughts)

Walrus operator in Python 3.8 is a feature that you should be already using right now. The assignment expression possibility allowed by this operator will be a great complement to the Python features. I am concerned that not everyone will not agree since to some people it will sacrifice readability. Nevertheless, I personally believe that with the right implementations, the walrus operator will be a great complement to your code.

Python 3.8中的Walrus运算符是您现在应该已经使用的功能。 该运算符允许的赋值表达式可能性将是Python功能的一个很好的补充。 我担心不是每个人都不会同意,因为某些人会牺牲可读性。 不过,我个人认为,有了正确的实现,海象运算符将对您的代码有很大的补充。

When using the walrus operator, just remember to use the parentheses to avoid ambiguity.

使用海象运算符时,请记住使用括号以避免歧义。

Regards,

问候,

Radian Krisno

拉迪安·克里斯诺(Radian Krisno)

程序员灯塔
转载请注明原文链接:海象运算符
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com