我们可以在条件下分配吗? [英] Can we have assignment in a condition?

查看:43
本文介绍了我们可以在条件下分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在条件中进行赋值?

例如

if (a=some_func()):# 用一个

解决方案

为什么不尝试一下?

<预><代码>>>>def some_func():...返回 2...>>>如果 (a = some_func()):文件

所以,不.

更新: 这是可能的(使用不同的语法) 在 Python 3.8 中

如果 a := some_func():

Is it possible to have assignment in a condition?

For ex.

if (a=some_func()):
    # Use a

解决方案

Why not try it out?

>>> def some_func():
...   return 2
... 
>>> if (a = some_func()):
  File "<stdin>", line 1
    if (a = some_func()):
          ^
SyntaxError: invalid syntax

So, no.

Update: This is possible (with different syntax) in Python 3.8

if a := some_func():

这篇关于我们可以在条件下分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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