python - 为什么 "is"关键字在这里不起作用?

标签 python integer

<分区>

我知道 is 用于比较两个对象是否相同,而 == 用于相等。根据我的经验, 总是适用于数字,因为 Python 重用了数字。例如:

>>>a = 3
>>>a is 3
True

每当我将某物与数字进行比较时,我习惯使用 is。但是 is 不适用于下面的这个程序:

from collections import namedtuple
# Code taken directly from [Udacity site][1].
# make a basic Link class
Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
                           'title', 'url'])

# list of Links to work with
links = [
    Link(0, 60398, 1334014208.0, 109,
         "C overtakes Java as the No. 1 programming language in the TIOBE index.",
         "http://pixelstech.net/article/index.php?id=1333969280"),
    Link(1, 60254, 1333962645.0, 891,
         "This explains why technical books are all ridiculously thick and overpriced",
         "http://prog21.dadgum.com/65.html"),
    Link(23, 62945, 1333894106.0, 351,
         "Learn Haskell Fast and Hard",
         "http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/"),
    Link(2, 6084, 1333996166.0, 81,
         "Announcing Yesod 1.0- a robust, developer friendly, high performance web framework for Haskell",
         "http://www.yesodweb.com/blog/2012/04/announcing-yesod-1-0"),
    Link(3, 30305, 1333968061.0, 270,
         "TIL about the Lisp Curse",
         "http://www.winestockwebdesign.com/Essays/Lisp_Curse.html"),
    Link(4, 59008, 1334016506.0, 19,
         "The Downfall of Imperative Programming. Functional Programming and the Multicore Revolution",
         "http://fpcomplete.com/the-downfall-of-imperative-programming/"),
    Link(5, 8712, 1333993676.0, 26,
         "Open Source - Twitter Stock Market Game - ",
         "http://www.twitstreet.com/"),
    Link(6, 48626, 1333975127.0, 63,
         "First look: Qt 5 makes JavaScript a first-class citizen for app development",
         "http://arstechnica.com/business/news/2012/04/an-in-depth-look-at-qt-5-making-javascript-a-first-class-citizen-for-native-cross-platform-developme.ars"),
    Link(7, 30172, 1334017294.0, 5,
         "Benchmark of Dictionary Structures", "http://lh3lh3.users.sourceforge.net/udb.shtml"),
    Link(8, 678, 1334014446.0, 7,
         "If It's Not on Prod, It Doesn't Count: The Value of Frequent Releases",
         "http://bits.shutterstock.com/?p=165"),
    Link(9, 29168, 1334006443.0, 18,
         "Language proposal: dave",
         "http://davelang.github.com/"),
    Link(17, 48626, 1334020271.0, 1,
         "LispNYC and EmacsNYC meetup Tuesday Night: Large Scale Development with Elisp ",
         "http://www.meetup.com/LispNYC/events/47373722/"),
    Link(101, 62443, 1334018620.0, 4,
         "research!rsc: Zip Files All The Way Down",
         "http://research.swtch.com/zip"),
    Link(12, 10262, 1334018169.0, 5,
         "The Tyranny of the Diff",
         "http://michaelfeathers.typepad.com/michael_feathers_blog/2012/04/the-tyranny-of-the-diff.html"),
    Link(13, 20831, 1333996529.0, 14,
         "Understanding NIO.2 File Channels in Java 7",
         "http://java.dzone.com/articles/understanding-nio2-file"),
    Link(15, 62443, 1333900877.0, 1244,
         "Why vector icons don't work",
         "http://www.pushing-pixels.org/2011/11/04/about-those-vector-icons.html"),
    Link(14, 30650, 1334013659.0, 3,
         "Python - Getting Data Into Graphite - Code Examples",
         "http://coreygoldberg.blogspot.com/2012/04/python-getting-data-into-graphite-code.html"),
    Link(16, 15330, 1333985877.0, 9,
         "Mozilla: The Web as the Platform and The Kilimanjaro Event",
         "https://groups.google.com/forum/?fromgroups#!topic/mozilla.dev.planning/Y9v46wFeejA"),
    Link(18, 62443, 1333939389.0, 104,
         "github is making me feel stupid(er)",
         "http://www.serpentine.com/blog/2012/04/08/github-is-making-me-feel-stupider/"),
    Link(19, 6937, 1333949857.0, 39,
         "BitC Retrospective: The Issues with Type Classes",
         "http://www.bitc-lang.org/pipermail/bitc-dev/2012-April/003315.html"),
    Link(20, 51067, 1333974585.0, 14,
         "Object Oriented C: Class-like Structures",
         "http://cecilsunkure.blogspot.com/2012/04/object-oriented-c-class-like-structures.html"),
    Link(10, 23944, 1333943632.0, 188,
         "The LOVE game framework version 0.8.0 has been released - with GLSL shader support!",
         "https://love2d.org/forums/viewtopic.php?f=3&amp;t=8750"),
    Link(22, 39191, 1334005674.0, 11,
         "An open letter to language designers: Please kill your sacred cows. (megarant)",
         "http://joshondesign.com/2012/03/09/open-letter-language-designers"),
    Link(21, 3777, 1333996565.0, 2,
         "Developers guide to Garage48 hackatron",
         "http://martingryner.com/developers-guide-to-garage48-hackatron/"),
    Link(24, 48626, 1333934004.0, 17,
         "An R programmer looks at Julia",
         "http://www.r-bloggers.com/an-r-programmer-looks-at-julia/")]


# links is a list of Link objects. Links have a handful of properties. For
# example, a Link's number of votes can be accessed by link.votes if "link" is a
# Link.

# make the function query() return a list of Links submitted by user 62443, by
# submission time ascending

def query():
    print "hello"
    print [link for link in links if link.submitter_id == 62443] # is does not work
    return sorted([link for link in links if link.submitter_id == 62443],key = lambda x: x[2])
query()

当我像这样在查询函数中使用 is[link for link in links if link.submitter_id is 62443] 我会得到一个空列表。但是如果我使用 ==,它工作正常。

在大多数情况下,代码是直接从 udacity 站点获取的,但我也在我的本地机器上尝试过。同样的结果。所以我认为在这种情况下数字现在是不同的对象,但为什么呢?有这个必要吗?

编辑:是的。我承认这个问题是重复的,应该关闭。但它与 first post 重复不是the second .在发布之前我不知道这个问题。

我的问题是我认为数字对象总是会被重用。

多亏了大家,我改掉了一个坏习惯。

最佳答案

如果不深入了解您正在使用的特定 Python 版本的实现细节,就无法回答您的问题。 没有关于是否a == b的定义暗示 a is b什么时候ab是数字。这通常是正确的,尤其是对于“小整数”,因为 CPython 保留了小整数对象的缓存,并且通常(不总是!)为给定的小整数值返回相同的对象。但是,没有任何关于它的定义、保证,甚至在各个版本中始终相同。

考虑内存地址可能会有点帮助,因为这就是 id() 的方式在 CPython 中实现。但是其他实现对 id() 使用不同的实现.例如,有人告诉我 id()在 Jython(用 Java 实现的 Python)中实现是一个主要的痛苦,因为 Java 可以在垃圾回收期间在内存中自由移动对象(CPython 不会:在 CPython 中,一个对象总是占用最初分配给它的内存,直到对象变成垃圾)。

唯一预期且受支持的用途 is是检查对象的两个名称是否实际上解析为同一个对象。例如,无论 b 的类型如何, 之后

a = b

一定是这样

a is b

True .这有时很有用。

_sentinel = object() # create a unique object

def somefunc(optional=_sentinel):
    if optional is _sentinel:  # we know for sure nothing was passed
        ...

另一个主要用途是让少数对象保证成为单例。 None , TrueFalse就是这方面的例子,而且确实是惯用的写法:

if a is None:

代替:

if a == None:

第一种方式成功当且仅当a实际上绑定(bind)到单例 None对象,但如果a,第二种方式可能会成功是满足 a.__eq__(None) 的任何类型返回 True .

不要使用 is对于数字。太疯狂了;-)

关于python - 为什么 "is"关键字在这里不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20674363/

相关文章:

python - 在 pandas python 列中获取非数字行

python - Python 中的参数优化

java - java 将字符串中的数字转换为列表

使用BLAS对整数类型进行矩阵乘法

python - 如何使用带有 phantomjs 的 Selenium 从新窗口获取 url

python - 类型错误:哈希值必须是 unicode 或字节,而不是 None

python - 如何在 Gekko 中获取目标函数的值

c++ - 检查 C/C++ 中的数字是否为整数(不使用 scanf/gets/etc)

python - 使用变量定义函数?

cocoa - 在核心数据中选择整数类型