zoukankan      html  css  js  c++  java
  • Is there a difference between `==` and `is` in Python?

    is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

    >>> a = [1, 2, 3]
    >>> b = a
    >>> b is a 
    True
    >>> b == a
    True
    >>> b = a[:]
    >>> b is a
    False
    >>> b == a
    True

    In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:

    >>> 1000 is 10**3
    False
    >>> 1000 == 10**3
    True

    The same holds true for string literals:

    >>> "a" is "a"
    True
    >>> "aa" is "a" * 2
    True
    >>> x = "a"
    >>> "aa" is x * 2
    False
    >>> "aa" is intern(x*2)
    True
  • 相关阅读:
    输入分隔符
    GO
    match|align|identify|cover_rate
    KEGG
    InterProScan
    Functional annotation
    GeneWise
    get middle lines
    goland debug web app with urfave cli
    go mod proxy
  • 原文地址:https://www.cnblogs.com/stefan-liu/p/5643402.html
Copyright © 2011-2022 走看看