python hasattr,Python的hasattr有时返回不正确的结果

python hasattr,Python的hasattr有时返回不正确的结果Whydoeshasat thaveafooatt gt gt gt classA object property deffoo self ErrorErrorEr gt gt gt a A gt gt gt h

python hasattr,Python的hasattr有时返回不正确的结果

Why does hasattr say that the instance doesn’t have a foo attribute?

>>> class A(object):

… @property

… def foo(self):

… ErrorErrorError

>>> a = A()

>>> hasattr(a, ‘foo’)

False

I expected:

>>> hasattr(a, ‘foo’)

NameError: name ‘ErrorErrorError’ is not defined`

解决方案

The python2 implementation of hasattr is fairly naive, it just tries to access that attribute and see whether it raises an exception or not.

Unfortunately, this means that any unhandled exceptions inside properties will get swallowed, and errors in that code can get lost. To add insult to injury, when hasattr eats the exception, it will also return an incorrect answer (here the attribute a.foo does exist, so the result should have returned True if anything).

In python3.2+, the behaviour has been corrected:

hasattr(object, name)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)

The fix is here, but unfortunately that change didn’t backport.

If the python2 behaviour causes trouble for you, consider to avoid using hasattr; instead you can use a try/except around getattr, catching only the AttributeError exception and letting any others raise unhandled.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216306.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:08
下一篇 2026年3月18日 下午12:08


相关推荐

  • js打开新窗口Window.open()方法

    js打开新窗口Window.open()方法目录 Window open 方法一 关闭当前页面 打开新的页面 1 方法一 利用超链接 2 方法二 利用 js 来跳转页面 在同当前窗口中打开窗口 二 保留当前页面 打开一个新的 tab 页面 1 方法一 利用超链接 2 方法二 利用 js 打开新页面 在另外新建窗口中打开窗口 三 保留当前页面 打开一个非 tab 页面完整的代码 window open page html newwindow height 100 width 400 top 0 left 0 toolbar no menub

    2026年3月18日
    2
  • Delphi源码:编辑长求字符串相似度

    Delphi源码:编辑长求字符串相似度

    2021年6月20日
    99
  • win10中安装centos7双系统

    win10中安装centos7双系统不能识别ntfs盘怎么处理。./configuremakemakeinstall用fdisk-l查看下分区表里ntfs盘都是什么盘。为这里在win10下的ntfs盘分别为sda1、sda2、sda3、sda5、sda6添加挂载在计算机下mnt文件下中新建几个文件夹分别用来挂在你的ntfs硬盘。这里我在mnt文件夹下新建了study、work、funmoun…

    2022年7月24日
    19
  • 数据库灾备

    数据库灾备数据库灾备数据是企业重要的生产资料 关键数据的丢失可能会给企业致命一击 因为数据是计算机系统存在的原因和基础 数据往往是不可再生的 一旦发生数据丢失 企业就会陷入困境 客户资料 技术文件 财务账目等客户 交易 生产数据可能被破坏得面目全非 概括起来 数据丢失分三个层次 逻辑错误 包括软件 bug 病毒攻击 数据块被破坏等 物理损坏 包括服务器 磁盘损坏等 自然灾害 火灾 地震等自然灾害对数

    2026年3月19日
    2
  • 遍历hashMap的5种方法

    遍历hashMap的5种方法1 使用 Iterator 遍历 HashMapEntry 使用 Iterator 遍历 HashMapKeySe 使用 For each 循环迭代 HashMap4 使用 Lambda 表达式遍历 HashMap5 使用 StreamAPI 遍历 HashMap1 使用 Iterator 遍历 HashMapEntry java tutorials iterations importjava util HashMap import

    2026年3月18日
    2
  • BZOJ 1798 [Ahoi2009]Seq 维护序列seq 线段树

    BZOJ 1798 [Ahoi2009]Seq 维护序列seq 线段树

    2022年2月5日
    31

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号