Python3基礎 is與== 區別

  •        Python : 3.7.3
  •          OS : Ubuntu 18.04.2 LTS
  •         IDE : pycharm-community-2019.1.3
  •       Conda : 4.7.5
  •    typesetting : Markdown

code

"""
@Author : 行初心
@Date   : 2019/7/7
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""


def main():
    # a與b的內容和地址都有差異
    a = [1, 2]
    b = [1]

    print(id(a), a)
    print(id(b), b)

    print()

    print("b.append(2)")
    b.append(2)
    # a與b的內容相同,地址有差異
    print(id(a), a)
    print(id(b), b)

    print()

    # 內存地址相同嗎?
    # is 比較的是兩個對象的id值是否相等
    print(a is b)
    print(id(a) == id(b))

    # 內容相同嗎?
    print(a == b)
    print(a.__eq__(b))


if __name__ == '__main__':
    main()

result

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/Base/demo.py
140337952333704 [1, 2]
140337952333768 [1]

b.append(2)
140337952333704 [1, 2]
140337952333768 [1, 2]

False
False
True
True

Process finished with exit code 0

source_code

def __eq__(self, *args, **kwargs): # real signature unknown
    """ Return self==value. """
    pass

def id(*args, **kwargs): # real signature unknown
    """
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
    """
    pass

reference

  • https://www.jb51.net/article/131559.htmhtml

  • https://www.cnblogs.com/lilz/p/9410319.html#_label2python

  • https://www.cnblogs.com/wangkun122/p/9082088.htmlgit

resource

  • [文檔 - English] docs.python.org/3
  • [文檔 - 中文] docs.python.org/zh-cn/3
  • [規範] www.python.org/dev/peps/pep-0008
  • [規範] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源碼] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平臺] www.cnblogs.com
  • [平臺] gitee.com


Python具備開源、跨平臺、解釋型、交互式等特性,值得學習。
Python的設計哲學:優雅,明確,簡單。提倡用一種方法,最好是隻有一種方法來作一件事。
代碼的書寫要遵照規範,這樣有助於溝通和理解。
每種語言都有獨特的思想,初學者須要轉變思惟、踏實踐行、堅持積累。bash

相關文章
相關標籤/搜索