V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hjq98765
V2EX  ›  Python

Python 里,什么对象能做到`a is a`返回`True`,而`a == a`返回`False`呢?

  •  
  •   hjq98765 · 2018-05-29 19:01:52 +08:00 · 4607 次点击
    这是一个创建于 2130 天前的主题,其中的信息可能已经有所发展或是发生改变。

    同事考我这个问题,我答不上来,特来求助

    >>> a is a
    True
    
    >>> a == a
    False
    
    第 1 条附言  ·  2018-05-31 10:23:02 +08:00

    同事给出的答案是:numpy.nanfloat("NaN"),试了一下确实是这样,2和3都支持

    math.nan是python3的特性,python2不支持

    感谢 @roy2220 @cxyfreedom @flowfire @whoami9894 提供正确答案!


    @stamaimer @ebingtel @zhongyiio @SingeeKing @goinghugh 几位用魔法方法开挂不算哈。。。

    不过作为小白还是学到了新东西,python原来还可以这么玩![捂脸]

    感谢几位提供思路!

    21 条回复    2018-06-01 09:16:32 +08:00
    lifeishort
        1
    lifeishort  
       2018-05-29 19:06:37 +08:00 via iPhone
    a=None 的时候是这样的
    stamaimer
        2
    stamaimer  
       2018-05-29 19:08:01 +08:00 via iPhone
    定义__equals__方法
    ebingtel
        3
    ebingtel  
       2018-05-29 19:10:30 +08:00
    @lifeishort @hjq98765
    Python 2.7

    >>> a = None
    >>> None is None
    True
    >>> None == None
    True
    >>> True == True
    True
    >>> class A(object):
    ... def __eq__(self, a):
    ... return self is not a
    ...
    >>> b = A()
    >>> b is b
    True
    >>> b ==b
    False
    roy2220
        4
    roy2220  
       2018-05-29 19:11:24 +08:00 via Android
    math.nan
    zhongyiio
        5
    zhongyiio  
       2018-05-29 19:13:38 +08:00
    可以参考下这个,不过没有你这个问题的答案。

    http://blog.jobbole.com/113631/
    lifeishort
        6
    lifeishort  
       2018-05-29 19:18:30 +08:00 via iPhone
    @ebingtel
    我记错了,感谢指正
    cxyfreedom
        7
    cxyfreedom  
       2018-05-29 19:35:59 +08:00 via iPhone
    自带的话 math.nan ,或者第三方库 numpy.NaN
    flowfire
        8
    flowfire  
       2018-05-29 19:39:10 +08:00 via iPhone
    NaN 试一下?
    jtsai
        9
    jtsai  
       2018-05-29 19:43:19 +08:00 via Android
    内存空间的问题 你 id () 一下
    SingeeKing
        10
    SingeeKing  
       2018-05-29 19:48:58 +08:00   ❤️ 1
    正常情况下是没有的。
    原因:is 是判断是不是一个( id 值相同),== 是判断是否相等(__eq__ 返回 True )
    按照常理,id 相同必相等

    但是确实可以。。。

    whoami9894
        11
    whoami9894  
       2018-05-29 20:02:44 +08:00 via Android
    ```python
    a = float("NaN")
    print(a==a)
    print(a is a)
    ```
    pkookp8
        12
    pkookp8  
       2018-05-29 20:12:24 +08:00 via Android
    @SingeeKing 这思路可以。。。
    codeeer
        13
    codeeer  
       2018-05-29 20:18:55 +08:00 via iPhone
    引用类型
    sammo
        14
    sammo  
       2018-05-29 20:20:49 +08:00 via Android
    C++ 就没这么多破事
    codehz
        15
    codehz  
       2018-05-29 23:37:10 +08:00
    @sammo #14 C++ 浮点数 NaN 也是自身不相等的,只要是按照 IEEE 标准设计的浮点数,都要满足 NaN != NaN
    phithon
        16
    phithon  
       2018-05-29 23:52:31 +08:00
    phithon
        17
    phithon  
       2018-05-29 23:53:12 +08:00
    想了个反的,哈
    goinghugh
        18
    goinghugh  
       2018-05-30 10:02:28 +08:00
    `is`判断是否是同一个对象,即 2 个的引用是否指向同一个对象;
    `==`返回的是`__eq__`的返回值。

    ![]( )

    在题主的问题里,只要是指向同一个对象,a is a 都是为 True,a==a 可以通过重写`__eq__`方法达成目的
    GeruzoniAnsasu
        19
    GeruzoniAnsasu  
       2018-05-30 11:40:13 +08:00
    @sammo
    @codehz
    感谢两位让我今天想例子的时候才发现原来浮点数不能作为 non-type 模板参数以及浮点数相除不是一个常量表达式。。
    lifeishort
        20
    lifeishort  
       2018-05-31 08:01:22 +08:00 via iPhone
    把 js 的 NAN 和 python 搞混了,尴尬的一楼。
    tukey
        21
    tukey  
       2018-06-01 09:16:32 +08:00
    mark
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1544 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.