热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

这个Python表达式有意义吗?-DoesthisPythonexpressionmakesense?

Ifoundthiskindofexpressionseveraltimesinapythonprogram:我在python程序中多次发现这种表达式:ifvariabl

I found this kind of expression several times in a python program:

我在python程序中多次发现这种表达式:

if variable is not None:
    dothings(variable)

It seems strange to me, and I think that it has no more sense than:

这对我来说似乎很奇怪,我认为它没有比以下更有意义了:

if variable:
    dothings(variable)

Maybe I don't know Python enough, and the expression is explained somewhere?

也许我不太了解Python,并且表达式在某处被解释了?

4 个解决方案

#1


14  

variable could be 0, or False, or [], or (); be 'falsy' in other words, and then the if statement would be skipped.

变量可以是0,或False,或[],或();换句话说,就是'falsy',然后会跳过if语句。

See Truth testing for more detail on what is considered false in a boolean context.

有关布尔上下文中被视为false的更多详细信息,请参阅真值测试。

In short, testing if variable is not None allows variable to be anything else, including values that would otherwise be considered False in a boolean context.

简而言之,测试变量是否不是None允许变量为其他任何东西,包括在布尔上下文中否则将被视为False的值。

#2


5  

Some values are falsy, but are not None. You may still want to dothings() to those values.

有些值是假的,但不是无。您可能仍希望将dothings()转换为这些值。

Here are the things that are falsy in Python 2. (You may want to change the '2' in the URL to a '3' to get the values for Python 3, but it didn't change much.)

以下是Python 2中伪造的东西。(您可能希望将URL中的'2'更改为'3'以获取Python 3的值,但它没有太大变化。)

#3


1  

E.g.

i = 0

if i is not None:
    print i    # prints i

if i:
    print i    # prints nothing

#4


0  

In the google voice python implementation I came across this as well, their use was during a function call. The parameters are defaulted to "None" and then when the function is called they can check if the values are changed or the default. I.E.

在谷歌语音python实现中,我也遇到了这个问题,他们的使用是在函数调用期间。参数默认为“None”,然后在调用函数时,可以检查值是否更改或默认值。 I.E.

def login (user=None, password=None)
  if user is None:
     user = input('Please enter your username');
  ...
  return ;

called by

login()

OR

login(user='cool_dude')

OR

any combination of user/password you wish.

您希望的任何用户/密码组合。

Additionally your "update" of the logic implies that variable == true or false. That is not correct for all cases (it may work in some cases but I'm throwing them out, since it's not a general case). What you are testing by using the "NONE" logic is whether the variable contains anything besides NONE. Similar to what I was saying above, all the "login function" was doing was determining if the user passed anything, not whether the value was valid, true, etc.

此外,您对逻辑的“更新”意味着变量== true或false。这对于所有情况都不正确(它可能在某些情况下有效,但我将它们抛弃,因为它不是一般情况)。您使用“NONE”逻辑测试的是变量是否包含除NONE之外的任何内容。类似于我上面所说的,所有“登录功能”正在做的是确定用户是否传递了任何内容,而不是该值是否有效,为真等。


推荐阅读
author-avatar
手机用户2502900723
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有