[{"data":1,"prerenderedAt":1512},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-id-function-explained":3},{"id":4,"title":5,"body":6,"description":1505,"extension":1506,"meta":1507,"navigation":108,"path":1508,"seo":1509,"stem":1510,"__hash__":1511},"content\u002Freference\u002Fpython-id-function-explained.md","Python id() Function Explained",{"type":7,"value":8,"toc":1478},"minimark",[9,19,26,32,47,137,147,158,166,172,175,195,204,208,211,229,232,243,246,354,357,361,366,389,391,509,512,543,554,558,561,649,652,662,668,675,679,682,792,794,808,811,825,833,840,845,859,865,986,988,1052,1055,1061,1067,1070,1092,1095,1171,1174,1207,1215,1219,1222,1240,1243,1255,1261,1265,1271,1298,1301,1375,1379,1387,1390,1397,1400,1407,1418,1425,1428,1435,1438,1442,1474],[10,11,13,14,18],"h1",{"id":12},"python-id-function-explained","Python ",[15,16,17],"code",{},"id()"," Function Explained",[20,21,22,23,25],"p",{},"The Python ",[15,24,17],{}," function returns a number that identifies an object while that object exists.",[20,27,28,29,31],{},"Beginners usually see ",[15,30,17],{}," when learning how variables and objects work in Python. It can help you understand whether two variables point to the same object, especially with mutable objects like lists and dictionaries.",[20,33,34,35,37,38,42,43,46],{},"Use ",[15,36,17],{}," for inspection and debugging. Do ",[39,40,41],"strong",{},"not"," use it to compare normal values. For value comparison, use ",[15,44,45],{},"==",".",[48,49,54],"pre",{"className":50,"code":51,"language":52,"meta":53,"style":53},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","name = \"Python\"\nprint(id(name))\n\nother = name\nprint(id(other))\n","python","",[15,55,56,80,103,110,121],{"__ignoreMap":53},[57,58,61,65,69,73,77],"span",{"class":59,"line":60},"line",1,[57,62,64],{"class":63},"su5hD","name ",[57,66,68],{"class":67},"smGrS","=",[57,70,72],{"class":71},"sjJ54"," \"",[57,74,76],{"class":75},"s_sjI","Python",[57,78,79],{"class":71},"\"\n",[57,81,83,87,91,94,96,100],{"class":59,"line":82},2,[57,84,86],{"class":85},"sptTA","print",[57,88,90],{"class":89},"sP7_E","(",[57,92,93],{"class":85},"id",[57,95,90],{"class":89},[57,97,99],{"class":98},"slqww","name",[57,101,102],{"class":89},"))\n",[57,104,106],{"class":59,"line":105},3,[57,107,109],{"emptyLinePlaceholder":108},true,"\n",[57,111,113,116,118],{"class":59,"line":112},4,[57,114,115],{"class":63},"other ",[57,117,68],{"class":67},[57,119,120],{"class":63}," name\n",[57,122,124,126,128,130,132,135],{"class":59,"line":123},5,[57,125,86],{"class":85},[57,127,90],{"class":89},[57,129,93],{"class":85},[57,131,90],{"class":89},[57,133,134],{"class":98},"other",[57,136,102],{"class":89},[20,138,139,140,143,144,146],{},"In this example, ",[15,141,142],{},"other = name"," makes both variables refer to the same object, so the two ",[15,145,17],{}," values match.",[148,149,150],"blockquote",{},[20,151,34,152,154,155,157],{},[15,153,17],{}," to inspect object identity. Do not use it to check whether two values are equal. Use ",[15,156,45],{}," for value comparison.",[159,160,162,163,165],"h2",{"id":161},"what-id-does","What ",[15,164,17],{}," does",[20,167,168,171],{},[15,169,170],{},"id(obj)"," returns an integer that identifies an object during its lifetime.",[20,173,174],{},"Key points:",[176,177,178,182,189,192],"ul",{},[179,180,181],"li",{},"The returned value is unique for that object while it exists",[179,183,184,185,188],{},"It shows ",[39,186,187],{},"object identity",", not object value",[179,190,191],{},"Beginners mostly use it for learning and debugging",[179,193,194],{},"It is helpful when you want to see whether two names refer to the same object",[20,196,197,198,203],{},"This is closely related to how ",[199,200,202],"a",{"href":201},"\u002Flearn\u002Fpython-variables-explained-for-beginners","Python variables work",". A variable does not store a value in the same way a box stores an item. Instead, it refers to an object.",[159,205,207],{"id":206},"basic-syntax","Basic syntax",[20,209,210],{},"The syntax is simple:",[48,212,214],{"className":50,"code":213,"language":52,"meta":53,"style":53},"id(object)\n",[15,215,216],{"__ignoreMap":53},[57,217,218,220,222,226],{"class":59,"line":60},[57,219,93],{"class":85},[57,221,90],{"class":89},[57,223,225],{"class":224},"sZMiF","object",[57,227,228],{"class":89},")\n",[20,230,231],{},"Important details:",[176,233,234,237,240],{},[179,235,236],{},"It takes one argument",[179,238,239],{},"It returns an integer",[179,241,242],{},"It works with strings, numbers, lists, dictionaries, functions, and other objects",[20,244,245],{},"Example:",[48,247,249],{"className":50,"code":248,"language":52,"meta":53,"style":53},"text = \"hello\"\nnumber = 42\nitems = [1, 2, 3]\n\nprint(id(text))\nprint(id(number))\nprint(id(items))\n",[15,250,251,265,276,303,307,322,338],{"__ignoreMap":53},[57,252,253,256,258,260,263],{"class":59,"line":60},[57,254,255],{"class":63},"text ",[57,257,68],{"class":67},[57,259,72],{"class":71},[57,261,262],{"class":75},"hello",[57,264,79],{"class":71},[57,266,267,270,272],{"class":59,"line":82},[57,268,269],{"class":63},"number ",[57,271,68],{"class":67},[57,273,275],{"class":274},"srdBf"," 42\n",[57,277,278,281,283,286,289,292,295,297,300],{"class":59,"line":105},[57,279,280],{"class":63},"items ",[57,282,68],{"class":67},[57,284,285],{"class":89}," [",[57,287,288],{"class":274},"1",[57,290,291],{"class":89},",",[57,293,294],{"class":274}," 2",[57,296,291],{"class":89},[57,298,299],{"class":274}," 3",[57,301,302],{"class":89},"]\n",[57,304,305],{"class":59,"line":112},[57,306,109],{"emptyLinePlaceholder":108},[57,308,309,311,313,315,317,320],{"class":59,"line":123},[57,310,86],{"class":85},[57,312,90],{"class":89},[57,314,93],{"class":85},[57,316,90],{"class":89},[57,318,319],{"class":98},"text",[57,321,102],{"class":89},[57,323,325,327,329,331,333,336],{"class":59,"line":324},6,[57,326,86],{"class":85},[57,328,90],{"class":89},[57,330,93],{"class":85},[57,332,90],{"class":89},[57,334,335],{"class":98},"number",[57,337,102],{"class":89},[57,339,341,343,345,347,349,352],{"class":59,"line":340},7,[57,342,86],{"class":85},[57,344,90],{"class":89},[57,346,93],{"class":85},[57,348,90],{"class":89},[57,350,351],{"class":98},"items",[57,353,102],{"class":89},[20,355,356],{},"Expected output will be three integers. The exact numbers will be different on your computer.",[159,358,360],{"id":359},"object-identity-vs-value","Object identity vs value",[20,362,363,364,46],{},"This is the most important idea behind ",[15,365,17],{},[176,367,368,376,384],{},[179,369,370,372,373],{},[15,371,45],{}," checks whether two objects have the same ",[39,374,375],{},"value",[179,377,378,381,382],{},[15,379,380],{},"is"," checks whether two variables refer to the same ",[39,383,225],{},[179,385,386,388],{},[15,387,17],{}," lets you inspect that identity as an integer",[20,390,245],{},[48,392,394],{"className":50,"code":393,"language":52,"meta":53,"style":53},"a = [1, 2, 3]\nb = [1, 2, 3]\n\nprint(a == b)   # True\nprint(a is b)   # False\nprint(id(a))\nprint(id(b))\n",[15,395,396,417,438,442,462,480,494],{"__ignoreMap":53},[57,397,398,401,403,405,407,409,411,413,415],{"class":59,"line":60},[57,399,400],{"class":63},"a ",[57,402,68],{"class":67},[57,404,285],{"class":89},[57,406,288],{"class":274},[57,408,291],{"class":89},[57,410,294],{"class":274},[57,412,291],{"class":89},[57,414,299],{"class":274},[57,416,302],{"class":89},[57,418,419,422,424,426,428,430,432,434,436],{"class":59,"line":82},[57,420,421],{"class":63},"b ",[57,423,68],{"class":67},[57,425,285],{"class":89},[57,427,288],{"class":274},[57,429,291],{"class":89},[57,431,294],{"class":274},[57,433,291],{"class":89},[57,435,299],{"class":274},[57,437,302],{"class":89},[57,439,440],{"class":59,"line":105},[57,441,109],{"emptyLinePlaceholder":108},[57,443,444,446,448,450,452,455,458],{"class":59,"line":112},[57,445,86],{"class":85},[57,447,90],{"class":89},[57,449,400],{"class":98},[57,451,45],{"class":67},[57,453,454],{"class":98}," b",[57,456,457],{"class":89},")",[57,459,461],{"class":460},"sutJx","   # True\n",[57,463,464,466,468,470,473,475,477],{"class":59,"line":123},[57,465,86],{"class":85},[57,467,90],{"class":89},[57,469,400],{"class":98},[57,471,380],{"class":472},"sVHd0",[57,474,454],{"class":98},[57,476,457],{"class":89},[57,478,479],{"class":460},"   # False\n",[57,481,482,484,486,488,490,492],{"class":59,"line":324},[57,483,86],{"class":85},[57,485,90],{"class":89},[57,487,93],{"class":85},[57,489,90],{"class":89},[57,491,199],{"class":98},[57,493,102],{"class":89},[57,495,496,498,500,502,504,507],{"class":59,"line":340},[57,497,86],{"class":85},[57,499,90],{"class":89},[57,501,93],{"class":85},[57,503,90],{"class":89},[57,505,506],{"class":98},"b",[57,508,102],{"class":89},[20,510,511],{},"Here:",[176,513,514,524,533],{},[179,515,516,519,520,523],{},[15,517,518],{},"a == b"," is ",[15,521,522],{},"True"," because both lists contain the same values",[179,525,526,519,529,532],{},[15,527,528],{},"a is b",[15,530,531],{},"False"," because they are different list objects",[179,534,535,538,539,542],{},[15,536,537],{},"id(a)"," and ",[15,540,541],{},"id(b)"," are usually different",[20,544,545,546,538,550,46],{},"If this topic feels confusing, it helps to read about ",[199,547,549],{"href":548},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types","mutability in Python",[199,551,553],{"href":552},"\u002Flearn\u002Fpython-data-types-overview","Python data types",[159,555,557],{"id":556},"simple-example-with-shared-references","Simple example with shared references",[20,559,560],{},"When you assign one variable to another, both names can refer to the same object.",[48,562,564],{"className":50,"code":563,"language":52,"meta":53,"style":53},"numbers = [10, 20, 30]\nother_numbers = numbers\n\nprint(id(numbers))\nprint(id(other_numbers))\nprint(numbers is other_numbers)\n",[15,565,566,590,600,604,619,634],{"__ignoreMap":53},[57,567,568,571,573,575,578,580,583,585,588],{"class":59,"line":60},[57,569,570],{"class":63},"numbers ",[57,572,68],{"class":67},[57,574,285],{"class":89},[57,576,577],{"class":274},"10",[57,579,291],{"class":89},[57,581,582],{"class":274}," 20",[57,584,291],{"class":89},[57,586,587],{"class":274}," 30",[57,589,302],{"class":89},[57,591,592,595,597],{"class":59,"line":82},[57,593,594],{"class":63},"other_numbers ",[57,596,68],{"class":67},[57,598,599],{"class":63}," numbers\n",[57,601,602],{"class":59,"line":105},[57,603,109],{"emptyLinePlaceholder":108},[57,605,606,608,610,612,614,617],{"class":59,"line":112},[57,607,86],{"class":85},[57,609,90],{"class":89},[57,611,93],{"class":85},[57,613,90],{"class":89},[57,615,616],{"class":98},"numbers",[57,618,102],{"class":89},[57,620,621,623,625,627,629,632],{"class":59,"line":123},[57,622,86],{"class":85},[57,624,90],{"class":89},[57,626,93],{"class":85},[57,628,90],{"class":89},[57,630,631],{"class":98},"other_numbers",[57,633,102],{"class":89},[57,635,636,638,640,642,644,647],{"class":59,"line":324},[57,637,86],{"class":85},[57,639,90],{"class":89},[57,641,570],{"class":98},[57,643,380],{"class":472},[57,645,646],{"class":98}," other_numbers",[57,648,228],{"class":89},[20,650,651],{},"Expected output:",[48,653,655],{"className":50,"code":654,"language":52,"meta":53,"style":53},"True\n",[15,656,657],{"__ignoreMap":53},[57,658,659],{"class":59,"line":60},[57,660,654],{"class":661},"s39Yj",[20,663,664,665,667],{},"The two ",[15,666,17],{}," values will also match.",[20,669,670,671,674],{},"This happens because ",[15,672,673],{},"other_numbers = numbers"," does not create a new list. It creates a second reference to the same list.",[159,676,678],{"id":677},"example-with-separate-objects","Example with separate objects",[20,680,681],{},"Now compare that with two separate lists that contain the same values.",[48,683,685],{"className":50,"code":684,"language":52,"meta":53,"style":53},"list_a = [1, 2, 3]\nlist_b = [1, 2, 3]\n\nprint(list_a == list_b)\nprint(list_a is list_b)\nprint(id(list_a))\nprint(id(list_b))\n",[15,686,687,708,729,733,748,762,777],{"__ignoreMap":53},[57,688,689,692,694,696,698,700,702,704,706],{"class":59,"line":60},[57,690,691],{"class":63},"list_a ",[57,693,68],{"class":67},[57,695,285],{"class":89},[57,697,288],{"class":274},[57,699,291],{"class":89},[57,701,294],{"class":274},[57,703,291],{"class":89},[57,705,299],{"class":274},[57,707,302],{"class":89},[57,709,710,713,715,717,719,721,723,725,727],{"class":59,"line":82},[57,711,712],{"class":63},"list_b ",[57,714,68],{"class":67},[57,716,285],{"class":89},[57,718,288],{"class":274},[57,720,291],{"class":89},[57,722,294],{"class":274},[57,724,291],{"class":89},[57,726,299],{"class":274},[57,728,302],{"class":89},[57,730,731],{"class":59,"line":105},[57,732,109],{"emptyLinePlaceholder":108},[57,734,735,737,739,741,743,746],{"class":59,"line":112},[57,736,86],{"class":85},[57,738,90],{"class":89},[57,740,691],{"class":98},[57,742,45],{"class":67},[57,744,745],{"class":98}," list_b",[57,747,228],{"class":89},[57,749,750,752,754,756,758,760],{"class":59,"line":123},[57,751,86],{"class":85},[57,753,90],{"class":89},[57,755,691],{"class":98},[57,757,380],{"class":472},[57,759,745],{"class":98},[57,761,228],{"class":89},[57,763,764,766,768,770,772,775],{"class":59,"line":324},[57,765,86],{"class":85},[57,767,90],{"class":89},[57,769,93],{"class":85},[57,771,90],{"class":89},[57,773,774],{"class":98},"list_a",[57,776,102],{"class":89},[57,778,779,781,783,785,787,790],{"class":59,"line":340},[57,780,86],{"class":85},[57,782,90],{"class":89},[57,784,93],{"class":85},[57,786,90],{"class":89},[57,788,789],{"class":98},"list_b",[57,791,102],{"class":89},[20,793,651],{},[48,795,797],{"className":50,"code":796,"language":52,"meta":53,"style":53},"True\nFalse\n",[15,798,799,803],{"__ignoreMap":53},[57,800,801],{"class":59,"line":60},[57,802,654],{"class":661},[57,804,805],{"class":59,"line":82},[57,806,807],{"class":661},"False\n",[20,809,810],{},"What this means:",[176,812,813,816,819],{},[179,814,815],{},"The lists are equal in value",[179,817,818],{},"They are not the same object",[179,820,821,822,824],{},"Their ",[15,823,17],{}," values are usually different",[20,826,827,828,830,831,46],{},"This is why ",[15,829,17],{}," should not replace ",[15,832,45],{},[159,834,836,837,839],{"id":835},"when-id-is-useful","When ",[15,838,17],{}," is useful",[20,841,842,844],{},[15,843,17],{}," can be useful in a few situations:",[176,846,847,850,853,856],{},[179,848,849],{},"Checking whether two variables refer to the same object",[179,851,852],{},"Understanding mutable objects like lists and dictionaries",[179,854,855],{},"Debugging code that changes data in place",[179,857,858],{},"Learning how assignment works in Python",[20,860,861,862,864],{},"For example, if a list changes unexpectedly, ",[15,863,17],{}," can help you see whether two variables point to the same list:",[48,866,868],{"className":50,"code":867,"language":52,"meta":53,"style":53},"original = [\"a\", \"b\"]\ncopy_ref = original\n\ncopy_ref.append(\"c\")\n\nprint(original)\nprint(copy_ref)\nprint(id(original))\nprint(id(copy_ref))\n",[15,869,870,896,906,910,931,935,946,956,971],{"__ignoreMap":53},[57,871,872,875,877,879,882,884,886,888,890,892,894],{"class":59,"line":60},[57,873,874],{"class":63},"original ",[57,876,68],{"class":67},[57,878,285],{"class":89},[57,880,881],{"class":71},"\"",[57,883,199],{"class":75},[57,885,881],{"class":71},[57,887,291],{"class":89},[57,889,72],{"class":71},[57,891,506],{"class":75},[57,893,881],{"class":71},[57,895,302],{"class":89},[57,897,898,901,903],{"class":59,"line":82},[57,899,900],{"class":63},"copy_ref ",[57,902,68],{"class":67},[57,904,905],{"class":63}," original\n",[57,907,908],{"class":59,"line":105},[57,909,109],{"emptyLinePlaceholder":108},[57,911,912,915,917,920,922,924,927,929],{"class":59,"line":112},[57,913,914],{"class":63},"copy_ref",[57,916,46],{"class":89},[57,918,919],{"class":98},"append",[57,921,90],{"class":89},[57,923,881],{"class":71},[57,925,926],{"class":75},"c",[57,928,881],{"class":71},[57,930,228],{"class":89},[57,932,933],{"class":59,"line":123},[57,934,109],{"emptyLinePlaceholder":108},[57,936,937,939,941,944],{"class":59,"line":324},[57,938,86],{"class":85},[57,940,90],{"class":89},[57,942,943],{"class":98},"original",[57,945,228],{"class":89},[57,947,948,950,952,954],{"class":59,"line":340},[57,949,86],{"class":85},[57,951,90],{"class":89},[57,953,914],{"class":98},[57,955,228],{"class":89},[57,957,959,961,963,965,967,969],{"class":59,"line":958},8,[57,960,86],{"class":85},[57,962,90],{"class":89},[57,964,93],{"class":85},[57,966,90],{"class":89},[57,968,943],{"class":98},[57,970,102],{"class":89},[57,972,974,976,978,980,982,984],{"class":59,"line":973},9,[57,975,86],{"class":85},[57,977,90],{"class":89},[57,979,93],{"class":85},[57,981,90],{"class":89},[57,983,914],{"class":98},[57,985,102],{"class":89},[20,987,651],{},[48,989,991],{"className":50,"code":990,"language":52,"meta":53,"style":53},"['a', 'b', 'c']\n['a', 'b', 'c']\n",[15,992,993,1024],{"__ignoreMap":53},[57,994,995,998,1001,1003,1005,1007,1010,1012,1014,1016,1018,1020,1022],{"class":59,"line":60},[57,996,997],{"class":89},"[",[57,999,1000],{"class":71},"'",[57,1002,199],{"class":75},[57,1004,1000],{"class":71},[57,1006,291],{"class":89},[57,1008,1009],{"class":71}," '",[57,1011,506],{"class":75},[57,1013,1000],{"class":71},[57,1015,291],{"class":89},[57,1017,1009],{"class":71},[57,1019,926],{"class":75},[57,1021,1000],{"class":71},[57,1023,302],{"class":89},[57,1025,1026,1028,1030,1032,1034,1036,1038,1040,1042,1044,1046,1048,1050],{"class":59,"line":82},[57,1027,997],{"class":89},[57,1029,1000],{"class":71},[57,1031,199],{"class":75},[57,1033,1000],{"class":71},[57,1035,291],{"class":89},[57,1037,1009],{"class":71},[57,1039,506],{"class":75},[57,1041,1000],{"class":71},[57,1043,291],{"class":89},[57,1045,1009],{"class":71},[57,1047,926],{"class":75},[57,1049,1000],{"class":71},[57,1051,302],{"class":89},[20,1053,1054],{},"Both variables show the same updated list because they refer to the same object.",[159,1056,1058,1059],{"id":1057},"when-not-to-use-id","When not to use ",[15,1060,17],{},[20,1062,1063,1064,1066],{},"Do not use ",[15,1065,17],{}," as a normal comparison tool.",[20,1068,1069],{},"Avoid these mistakes:",[176,1071,1072,1078,1084,1087],{},[179,1073,1074,1075,1077],{},"Do not use it instead of ",[15,1076,45],{}," for strings, numbers, lists, or other normal values",[179,1079,1080,1081,1083],{},"Do not store or depend on ",[15,1082,17],{}," values across program runs",[179,1085,1086],{},"Do not assume the integer has a meaning you should interpret",[179,1088,1089,1090],{},"Do not build unnecessary program logic around ",[15,1091,17],{},[20,1093,1094],{},"Bad example:",[48,1096,1098],{"className":50,"code":1097,"language":52,"meta":53,"style":53},"a = \"cat\"\nb = \"cat\"\n\nif id(a) == id(b):\n    print(\"The values are equal\")\n",[15,1099,1100,1113,1125,1129,1155],{"__ignoreMap":53},[57,1101,1102,1104,1106,1108,1111],{"class":59,"line":60},[57,1103,400],{"class":63},[57,1105,68],{"class":67},[57,1107,72],{"class":71},[57,1109,1110],{"class":75},"cat",[57,1112,79],{"class":71},[57,1114,1115,1117,1119,1121,1123],{"class":59,"line":82},[57,1116,421],{"class":63},[57,1118,68],{"class":67},[57,1120,72],{"class":71},[57,1122,1110],{"class":75},[57,1124,79],{"class":71},[57,1126,1127],{"class":59,"line":105},[57,1128,109],{"emptyLinePlaceholder":108},[57,1130,1131,1134,1137,1139,1141,1143,1146,1148,1150,1152],{"class":59,"line":112},[57,1132,1133],{"class":472},"if",[57,1135,1136],{"class":85}," id",[57,1138,90],{"class":89},[57,1140,199],{"class":98},[57,1142,457],{"class":89},[57,1144,1145],{"class":67}," ==",[57,1147,1136],{"class":85},[57,1149,90],{"class":89},[57,1151,506],{"class":98},[57,1153,1154],{"class":89},"):\n",[57,1156,1157,1160,1162,1164,1167,1169],{"class":59,"line":123},[57,1158,1159],{"class":85},"    print",[57,1161,90],{"class":89},[57,1163,881],{"class":71},[57,1165,1166],{"class":75},"The values are equal",[57,1168,881],{"class":71},[57,1170,228],{"class":89},[20,1172,1173],{},"This is the wrong test. Use:",[48,1175,1177],{"className":50,"code":1176,"language":52,"meta":53,"style":53},"if a == b:\n    print(\"The values are equal\")\n",[15,1178,1179,1193],{"__ignoreMap":53},[57,1180,1181,1183,1186,1188,1190],{"class":59,"line":60},[57,1182,1133],{"class":472},[57,1184,1185],{"class":63}," a ",[57,1187,45],{"class":67},[57,1189,454],{"class":63},[57,1191,1192],{"class":89},":\n",[57,1194,1195,1197,1199,1201,1203,1205],{"class":59,"line":82},[57,1196,1159],{"class":85},[57,1198,90],{"class":89},[57,1200,881],{"class":71},[57,1202,1166],{"class":75},[57,1204,881],{"class":71},[57,1206,228],{"class":89},[20,1208,34,1209,1211,1212,1214],{},[15,1210,380],{}," or ",[15,1213,17],{}," only when identity actually matters.",[159,1216,1218],{"id":1217},"important-beginner-notes","Important beginner notes",[20,1220,1221],{},"There are a few details that can confuse beginners:",[176,1223,1224,1227,1230,1235],{},[179,1225,1226],{},"Some small values may appear to share identities because Python can reuse objects",[179,1228,1229],{},"That behavior can vary between Python implementations",[179,1231,1232,1233],{},"Do not assume equal immutable values always have the same ",[15,1234,17],{},[179,1236,1237,1238],{},"Do not assume they always have different ",[15,1239,17],{},[20,1241,1242],{},"The important rule is:",[176,1244,1245,1250],{},[179,1246,34,1247,1249],{},[15,1248,45],{}," to check value",[179,1251,34,1252,1254],{},[15,1253,380],{}," to check identity",[20,1256,1257,1258,1260],{},"In everyday Python code, ",[15,1259,17],{}," is mainly an inspection tool. It is not something you use all the time.",[159,1262,1264],{"id":1263},"common-mistakes","Common mistakes",[20,1266,1267,1268,1270],{},"Here are common beginner mistakes with ",[15,1269,17],{},":",[176,1272,1273,1282,1285,1288,1293],{},[179,1274,1275,1276,1278,1279,1281],{},"Using ",[15,1277,17],{}," when ",[15,1280,45],{}," should be used",[179,1283,1284],{},"Confusing identity with equality",[179,1286,1287],{},"Thinking the returned integer is a memory address you should rely on",[179,1289,1290,1291],{},"Assuming equal immutable values always have different or always have the same ",[15,1292,17],{},[179,1294,1275,1295,1297],{},[15,1296,17],{}," to make program logic decisions unnecessarily",[20,1299,1300],{},"If you are debugging, these commands are often helpful:",[48,1302,1304],{"className":50,"code":1303,"language":52,"meta":53,"style":53},"print(id(my_object))\nprint(a == b)\nprint(a is b)\nprint(type(my_object))\nhelp(id)\n",[15,1305,1306,1321,1335,1349,1364],{"__ignoreMap":53},[57,1307,1308,1310,1312,1314,1316,1319],{"class":59,"line":60},[57,1309,86],{"class":85},[57,1311,90],{"class":89},[57,1313,93],{"class":85},[57,1315,90],{"class":89},[57,1317,1318],{"class":98},"my_object",[57,1320,102],{"class":89},[57,1322,1323,1325,1327,1329,1331,1333],{"class":59,"line":82},[57,1324,86],{"class":85},[57,1326,90],{"class":89},[57,1328,400],{"class":98},[57,1330,45],{"class":67},[57,1332,454],{"class":98},[57,1334,228],{"class":89},[57,1336,1337,1339,1341,1343,1345,1347],{"class":59,"line":105},[57,1338,86],{"class":85},[57,1340,90],{"class":89},[57,1342,400],{"class":98},[57,1344,380],{"class":472},[57,1346,454],{"class":98},[57,1348,228],{"class":89},[57,1350,1351,1353,1355,1358,1360,1362],{"class":59,"line":112},[57,1352,86],{"class":85},[57,1354,90],{"class":89},[57,1356,1357],{"class":224},"type",[57,1359,90],{"class":89},[57,1361,1318],{"class":98},[57,1363,102],{"class":89},[57,1365,1366,1369,1371,1373],{"class":59,"line":123},[57,1367,1368],{"class":85},"help",[57,1370,90],{"class":89},[57,1372,93],{"class":85},[57,1374,228],{"class":89},[159,1376,1378],{"id":1377},"faq","FAQ",[1380,1381,1383,1384,1386],"h3",{"id":1382},"what-does-python-id-return","What does Python ",[15,1385,17],{}," return?",[20,1388,1389],{},"It returns an integer that identifies an object for as long as that object exists.",[1380,1391,1393,1394,1396],{"id":1392},"is-id-the-same-as-a-memory-address","Is ",[15,1395,17],{}," the same as a memory address?",[20,1398,1399],{},"In CPython, it is often related to the memory address, but beginners should treat it as an identity value, not as something to depend on.",[1380,1401,1403,1404,1406],{"id":1402},"should-i-use-id-to-compare-two-values","Should I use ",[15,1405,17],{}," to compare two values?",[20,1408,1409,1410,1412,1413,1211,1415,1417],{},"No. Use ",[15,1411,45],{}," to compare values. Use ",[15,1414,17],{},[15,1416,380],{}," only when you need to know whether two variables refer to the same object.",[1380,1419,1421,1422,1424],{"id":1420},"why-do-two-variables-sometimes-have-the-same-id","Why do two variables sometimes have the same ",[15,1423,17],{},"?",[20,1426,1427],{},"Because they can point to the same object.",[1380,1429,1431,1432,1434],{"id":1430},"why-do-equal-lists-have-different-id-values","Why do equal lists have different ",[15,1433,17],{}," values?",[20,1436,1437],{},"Because they can store the same values but still be separate objects.",[159,1439,1441],{"id":1440},"see-also","See also",[176,1443,1444,1449,1454,1459,1468],{},[179,1445,1446],{},[199,1447,1448],{"href":201},"Python variables explained for beginners",[179,1450,1451],{},[199,1452,1453],{"href":548},"Mutability in Python: mutable vs immutable types",[179,1455,1456],{},[199,1457,1458],{"href":552},"Python data types overview",[179,1460,1461],{},[199,1462,13,1464,1467],{"href":1463},"\u002Freference\u002Fpython-isinstance-function-explained",[15,1465,1466],{},"isinstance()"," function explained",[179,1469,1470],{},[199,1471,1473],{"href":1472},"\u002Flearn\u002Fpython-functions-explained","Python functions explained",[1475,1476,1477],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":53,"searchDepth":82,"depth":82,"links":1479},[1480,1482,1483,1484,1485,1486,1488,1490,1491,1492,1504],{"id":161,"depth":82,"text":1481},"What id() does",{"id":206,"depth":82,"text":207},{"id":359,"depth":82,"text":360},{"id":556,"depth":82,"text":557},{"id":677,"depth":82,"text":678},{"id":835,"depth":82,"text":1487},"When id() is useful",{"id":1057,"depth":82,"text":1489},"When not to use id()",{"id":1217,"depth":82,"text":1218},{"id":1263,"depth":82,"text":1264},{"id":1377,"depth":82,"text":1378,"children":1493},[1494,1496,1498,1500,1502],{"id":1382,"depth":105,"text":1495},"What does Python id() return?",{"id":1392,"depth":105,"text":1497},"Is id() the same as a memory address?",{"id":1402,"depth":105,"text":1499},"Should I use id() to compare two values?",{"id":1420,"depth":105,"text":1501},"Why do two variables sometimes have the same id()?",{"id":1430,"depth":105,"text":1503},"Why do equal lists have different id() values?",{"id":1440,"depth":82,"text":1441},"Master python id function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-id-function-explained",{"title":5,"description":1505},"reference\u002Fpython-id-function-explained","urpvIG90OiBF3OAxMd-CGWa-9xnfjJsWk0ZM8U_FOZA",1777585495762]