热门标签 | 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之外的任何内容。类似于我上面所说的,所有“登录功能”正在做的是确定用户是否传递了任何内容,而不是该值是否有效,为真等。


推荐阅读
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文介绍如何从字符串中移除大写、小写、特殊、数字和非数字字符,并提供了多种编程语言的实现示例。 ... [详细]
  • 1、字符型常量字符型常量指单个字符,是用一对单引号及其所括起来的字符表示。例如:‘A’、‘a’、‘0’、’$‘等都是字符型常量。C语言的字符使用的就是 ... [详细]
  • 本文详细介绍了 Python 中的 with 语句及其背后的上下文管理器机制,从基本概念入手,通过具体示例和原理分析,帮助读者深入理解这一重要的资源管理工具。 ... [详细]
  • 利用 Jest 和 Supertest 实现接口测试的全面指南
    本文深入探讨了如何使用 Jest 和 Supertest 进行接口测试,通过实际案例详细解析了测试环境的搭建、测试用例的编写以及异步测试的处理方法。 ... [详细]
  • 以下代码仅适用于没有引用参数的调用方法。 publicdelegatevoidtestD2(paramsobject[]args) ... [详细]
  • 本文将探讨从ASP.NET 1.1到2.0期间编译系统的重要变革。通过对比两个版本的即时编译模型,我们将揭示2.0版本中引入的新特性和改进之处。 ... [详细]
  • 本教程将深入探讨C#编程语言中的条件控制结构,包括if语句和switch语句的使用方法。通过本课的学习,您将掌握如何利用这些控制结构来实现程序的条件分支逻辑。 ... [详细]
  • 本文基于《Linux命令行与Shell脚本编程大全》第三版的第十一章内容,探讨了如何构建基本的Shell脚本,包括命令组合、脚本创建、消息显示、变量使用、输入输出重定向、管道、数学运算及脚本退出等方面的知识。 ... [详细]
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社区 版权所有