[{"data":1,"prerenderedAt":1970},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-method-in-python":3},{"id":4,"title":5,"body":6,"description":1963,"extension":1964,"meta":1965,"navigation":169,"path":1966,"seo":1967,"stem":1968,"__hash__":1969},"content\u002Fglossary\u002Fwhat-is-a-method-in-python.md","What Is a Method in Python?",{"type":7,"value":8,"toc":1935},"minimark",[9,13,17,20,78,85,95,100,103,106,124,127,184,187,196,199,218,225,233,237,240,260,262,356,358,390,393,411,414,421,425,428,433,436,451,453,520,522,543,550,554,557,574,576,673,675,721,725,728,745,747,855,857,931,934,938,941,973,976,991,994,996,1062,1064,1073,1075,1096,1099,1111,1115,1118,1129,1131,1211,1213,1222,1224,1246,1253,1259,1327,1329,1338,1343,1349,1352,1486,1488,1497,1503,1507,1510,1514,1517,1519,1576,1578,1618,1621,1625,1628,1630,1669,1675,1689,1693,1696,1751,1754,1758,1761,1828,1831,1851,1855,1859,1862,1866,1873,1877,1880,1884,1889,1893,1896,1900,1931],[10,11,5],"h1",{"id":12},"what-is-a-method-in-python",[14,15,16],"p",{},"A method in Python is a function that belongs to an object.",[14,18,19],{},"You use methods all the time in Python, even in simple code like:",[21,22,27],"pre",{"className":23,"code":24,"language":25,"meta":26,"style":26},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","name = \"Ada\"\nprint(name.upper())\n","python","",[28,29,30,54],"code",{"__ignoreMap":26},[31,32,35,39,43,47,51],"span",{"class":33,"line":34},"line",1,[31,36,38],{"class":37},"su5hD","name ",[31,40,42],{"class":41},"smGrS","=",[31,44,46],{"class":45},"sjJ54"," \"",[31,48,50],{"class":49},"s_sjI","Ada",[31,52,53],{"class":45},"\"\n",[31,55,57,61,65,69,72,75],{"class":33,"line":56},2,[31,58,60],{"class":59},"sptTA","print",[31,62,64],{"class":63},"sP7_E","(",[31,66,68],{"class":67},"slqww","name",[31,70,71],{"class":63},".",[31,73,74],{"class":67},"upper",[31,76,77],{"class":63},"())\n",[14,79,80,81,84],{},"In this example, ",[28,82,83],{},"upper()"," is a method of a string object.",[14,86,87,88,91,92,71],{},"Understanding methods helps you read Python code more easily. It also helps you know why some actions are written as ",[28,89,90],{},"object.method()"," while others are written as regular functions like ",[28,93,94],{},"len(object)",[96,97,99],"h2",{"id":98},"definition","Definition",[14,101,102],{},"A method is a function that belongs to an object.",[14,104,105],{},"Key points:",[107,108,109,113,116,121],"ul",{},[110,111,112],"li",{},"A method is called on an object",[110,114,115],{},"You use dot notation to call it",[110,117,118,119],{},"The usual shape is ",[28,120,90],{},[110,122,123],{},"Methods can perform an action or return information",[14,125,126],{},"Example:",[21,128,130],{"className":23,"code":129,"language":25,"meta":26,"style":26},"text = \" hello \"\ncleaned = text.strip()\n\nprint(cleaned)\n",[28,131,132,146,164,171],{"__ignoreMap":26},[31,133,134,137,139,141,144],{"class":33,"line":34},[31,135,136],{"class":37},"text ",[31,138,42],{"class":41},[31,140,46],{"class":45},[31,142,143],{"class":49}," hello ",[31,145,53],{"class":45},[31,147,148,151,153,156,158,161],{"class":33,"line":56},[31,149,150],{"class":37},"cleaned ",[31,152,42],{"class":41},[31,154,155],{"class":37}," text",[31,157,71],{"class":63},[31,159,160],{"class":67},"strip",[31,162,163],{"class":63},"()\n",[31,165,167],{"class":33,"line":166},3,[31,168,170],{"emptyLinePlaceholder":169},true,"\n",[31,172,174,176,178,181],{"class":33,"line":173},4,[31,175,60],{"class":59},[31,177,64],{"class":63},[31,179,180],{"class":67},"cleaned",[31,182,183],{"class":63},")\n",[14,185,186],{},"Output:",[21,188,190],{"className":23,"code":189,"language":25,"meta":26,"style":26},"hello\n",[28,191,192],{"__ignoreMap":26},[31,193,194],{"class":33,"line":34},[31,195,189],{"class":37},[14,197,198],{},"Here:",[107,200,201,207,212],{},[110,202,203,206],{},[28,204,205],{},"text"," is the object",[110,208,209,211],{},[28,210,160],{}," is the method name",[110,213,214,217],{},[28,215,216],{},"()"," runs the method",[14,219,220,221,224],{},"The ",[28,222,223],{},"strip()"," method removes spaces from the beginning and end of the string.",[14,226,227,228,71],{},"If you are new to objects, see ",[229,230,232],"a",{"href":231},"\u002Fglossary\u002Fwhat-is-an-object-in-python\u002F","what an object is in Python",[96,234,236],{"id":235},"how-methods-are-different-from-functions","How methods are different from functions",[14,238,239],{},"A method is similar to a function, but they are not called in the same way.",[107,241,242,248,254,257],{},[110,243,244,245],{},"A function is called by name, like ",[28,246,247],{},"len(my_list)",[110,249,250,251],{},"A method is called on an object, like ",[28,252,253],{},"my_list.append(5)",[110,255,256],{},"Methods belong to a specific type or class",[110,258,259],{},"Not every operation in Python uses a method",[14,261,126],{},[21,263,265],{"className":23,"code":264,"language":25,"meta":26,"style":26},"numbers = [1, 2, 3]\n\nprint(len(numbers))   # function\nnumbers.append(4)     # method\n\nprint(numbers)\n",[28,266,267,295,299,320,340,345],{"__ignoreMap":26},[31,268,269,272,274,277,281,284,287,289,292],{"class":33,"line":34},[31,270,271],{"class":37},"numbers ",[31,273,42],{"class":41},[31,275,276],{"class":63}," [",[31,278,280],{"class":279},"srdBf","1",[31,282,283],{"class":63},",",[31,285,286],{"class":279}," 2",[31,288,283],{"class":63},[31,290,291],{"class":279}," 3",[31,293,294],{"class":63},"]\n",[31,296,297],{"class":33,"line":56},[31,298,170],{"emptyLinePlaceholder":169},[31,300,301,303,305,308,310,313,316],{"class":33,"line":166},[31,302,60],{"class":59},[31,304,64],{"class":63},[31,306,307],{"class":59},"len",[31,309,64],{"class":63},[31,311,312],{"class":67},"numbers",[31,314,315],{"class":63},"))",[31,317,319],{"class":318},"sutJx","   # function\n",[31,321,322,324,326,329,331,334,337],{"class":33,"line":173},[31,323,312],{"class":37},[31,325,71],{"class":63},[31,327,328],{"class":67},"append",[31,330,64],{"class":63},[31,332,333],{"class":279},"4",[31,335,336],{"class":63},")",[31,338,339],{"class":318},"     # method\n",[31,341,343],{"class":33,"line":342},5,[31,344,170],{"emptyLinePlaceholder":169},[31,346,348,350,352,354],{"class":33,"line":347},6,[31,349,60],{"class":59},[31,351,64],{"class":63},[31,353,312],{"class":67},[31,355,183],{"class":63},[14,357,186],{},[21,359,361],{"className":23,"code":360,"language":25,"meta":26,"style":26},"3\n[1, 2, 3, 4]\n",[28,362,363,368],{"__ignoreMap":26},[31,364,365],{"class":33,"line":34},[31,366,367],{"class":279},"3\n",[31,369,370,373,375,377,379,381,383,385,388],{"class":33,"line":56},[31,371,372],{"class":63},"[",[31,374,280],{"class":279},[31,376,283],{"class":63},[31,378,286],{"class":279},[31,380,283],{"class":63},[31,382,291],{"class":279},[31,384,283],{"class":63},[31,386,387],{"class":279}," 4",[31,389,294],{"class":63},[14,391,392],{},"In this example:",[107,394,395,403],{},[110,396,397,400,401],{},[28,398,399],{},"len(numbers)"," uses the built-in function ",[28,402,307],{},[110,404,405,408,409],{},[28,406,407],{},"numbers.append(4)"," uses the list method ",[28,410,328],{},[14,412,413],{},"So even though both work with the list, one is a function and one is a method.",[14,415,416,417,71],{},"For a practical example, see the ",[229,418,420],{"href":419},"\u002Freference\u002Fpython-list-append-method\u002F","Python list append() method",[96,422,424],{"id":423},"simple-examples-of-methods","Simple examples of methods",[14,426,427],{},"Different object types have different methods.",[429,430,432],"h3",{"id":431},"string-methods","String methods",[14,434,435],{},"Strings have methods such as:",[107,437,438,443,447],{},[110,439,440],{},[28,441,442],{},"lower()",[110,444,445],{},[28,446,83],{},[110,448,449],{},[28,450,223],{},[14,452,126],{},[21,454,456],{"className":23,"code":455,"language":25,"meta":26,"style":26},"word = \" PyThOn \"\n\nprint(word.lower())\nprint(word.upper())\nprint(word.strip())\n",[28,457,458,472,476,492,506],{"__ignoreMap":26},[31,459,460,463,465,467,470],{"class":33,"line":34},[31,461,462],{"class":37},"word ",[31,464,42],{"class":41},[31,466,46],{"class":45},[31,468,469],{"class":49}," PyThOn ",[31,471,53],{"class":45},[31,473,474],{"class":33,"line":56},[31,475,170],{"emptyLinePlaceholder":169},[31,477,478,480,482,485,487,490],{"class":33,"line":166},[31,479,60],{"class":59},[31,481,64],{"class":63},[31,483,484],{"class":67},"word",[31,486,71],{"class":63},[31,488,489],{"class":67},"lower",[31,491,77],{"class":63},[31,493,494,496,498,500,502,504],{"class":33,"line":173},[31,495,60],{"class":59},[31,497,64],{"class":63},[31,499,484],{"class":67},[31,501,71],{"class":63},[31,503,74],{"class":67},[31,505,77],{"class":63},[31,507,508,510,512,514,516,518],{"class":33,"line":342},[31,509,60],{"class":59},[31,511,64],{"class":63},[31,513,484],{"class":67},[31,515,71],{"class":63},[31,517,160],{"class":67},[31,519,77],{"class":63},[14,521,186],{},[21,523,525],{"className":23,"code":524,"language":25,"meta":26,"style":26}," python \n PYTHON\nPyThOn\n",[28,526,527,532,538],{"__ignoreMap":26},[31,528,529],{"class":33,"line":34},[31,530,531],{"class":37}," python \n",[31,533,534],{"class":33,"line":56},[31,535,537],{"class":536},"s_hVV"," PYTHON\n",[31,539,540],{"class":33,"line":166},[31,541,542],{"class":37},"PyThOn\n",[14,544,545,546,71],{},"If you want to learn one of these in more detail, see the ",[229,547,549],{"href":548},"\u002Freference\u002Fpython-string-lower-method\u002F","Python string lower() method",[429,551,553],{"id":552},"list-methods","List methods",[14,555,556],{},"Lists have methods such as:",[107,558,559,564,569],{},[110,560,561],{},[28,562,563],{},"append()",[110,565,566],{},[28,567,568],{},"pop()",[110,570,571],{},[28,572,573],{},"sort()",[14,575,126],{},[21,577,579],{"className":23,"code":578,"language":25,"meta":26,"style":26},"numbers = [3, 1, 2]\n\nnumbers.append(4)\nnumbers.sort()\n\nprint(numbers)\nprint(numbers.pop())\nprint(numbers)\n",[28,580,581,603,607,621,632,636,646,662],{"__ignoreMap":26},[31,582,583,585,587,589,592,594,597,599,601],{"class":33,"line":34},[31,584,271],{"class":37},[31,586,42],{"class":41},[31,588,276],{"class":63},[31,590,591],{"class":279},"3",[31,593,283],{"class":63},[31,595,596],{"class":279}," 1",[31,598,283],{"class":63},[31,600,286],{"class":279},[31,602,294],{"class":63},[31,604,605],{"class":33,"line":56},[31,606,170],{"emptyLinePlaceholder":169},[31,608,609,611,613,615,617,619],{"class":33,"line":166},[31,610,312],{"class":37},[31,612,71],{"class":63},[31,614,328],{"class":67},[31,616,64],{"class":63},[31,618,333],{"class":279},[31,620,183],{"class":63},[31,622,623,625,627,630],{"class":33,"line":173},[31,624,312],{"class":37},[31,626,71],{"class":63},[31,628,629],{"class":67},"sort",[31,631,163],{"class":63},[31,633,634],{"class":33,"line":342},[31,635,170],{"emptyLinePlaceholder":169},[31,637,638,640,642,644],{"class":33,"line":347},[31,639,60],{"class":59},[31,641,64],{"class":63},[31,643,312],{"class":67},[31,645,183],{"class":63},[31,647,649,651,653,655,657,660],{"class":33,"line":648},7,[31,650,60],{"class":59},[31,652,64],{"class":63},[31,654,312],{"class":67},[31,656,71],{"class":63},[31,658,659],{"class":67},"pop",[31,661,77],{"class":63},[31,663,665,667,669,671],{"class":33,"line":664},8,[31,666,60],{"class":59},[31,668,64],{"class":63},[31,670,312],{"class":67},[31,672,183],{"class":63},[14,674,186],{},[21,676,678],{"className":23,"code":677,"language":25,"meta":26,"style":26},"[1, 2, 3, 4]\n4\n[1, 2, 3]\n",[28,679,680,700,705],{"__ignoreMap":26},[31,681,682,684,686,688,690,692,694,696,698],{"class":33,"line":34},[31,683,372],{"class":63},[31,685,280],{"class":279},[31,687,283],{"class":63},[31,689,286],{"class":279},[31,691,283],{"class":63},[31,693,291],{"class":279},[31,695,283],{"class":63},[31,697,387],{"class":279},[31,699,294],{"class":63},[31,701,702],{"class":33,"line":56},[31,703,704],{"class":279},"4\n",[31,706,707,709,711,713,715,717,719],{"class":33,"line":166},[31,708,372],{"class":63},[31,710,280],{"class":279},[31,712,283],{"class":63},[31,714,286],{"class":279},[31,716,283],{"class":63},[31,718,291],{"class":279},[31,720,294],{"class":63},[429,722,724],{"id":723},"dictionary-methods","Dictionary methods",[14,726,727],{},"Dictionaries have methods such as:",[107,729,730,735,740],{},[110,731,732],{},[28,733,734],{},"get()",[110,736,737],{},[28,738,739],{},"keys()",[110,741,742],{},[28,743,744],{},"items()",[14,746,126],{},[21,748,750],{"className":23,"code":749,"language":25,"meta":26,"style":26},"user = {\"name\": \"Sam\", \"age\": 20}\n\nprint(user.get(\"name\"))\nprint(user.keys())\nprint(user.items())\n",[28,751,752,796,800,825,840],{"__ignoreMap":26},[31,753,754,757,759,762,765,767,769,772,774,777,779,781,783,786,788,790,793],{"class":33,"line":34},[31,755,756],{"class":37},"user ",[31,758,42],{"class":41},[31,760,761],{"class":63}," {",[31,763,764],{"class":45},"\"",[31,766,68],{"class":49},[31,768,764],{"class":45},[31,770,771],{"class":63},":",[31,773,46],{"class":45},[31,775,776],{"class":49},"Sam",[31,778,764],{"class":45},[31,780,283],{"class":63},[31,782,46],{"class":45},[31,784,785],{"class":49},"age",[31,787,764],{"class":45},[31,789,771],{"class":63},[31,791,792],{"class":279}," 20",[31,794,795],{"class":63},"}\n",[31,797,798],{"class":33,"line":56},[31,799,170],{"emptyLinePlaceholder":169},[31,801,802,804,806,809,811,814,816,818,820,822],{"class":33,"line":166},[31,803,60],{"class":59},[31,805,64],{"class":63},[31,807,808],{"class":67},"user",[31,810,71],{"class":63},[31,812,813],{"class":67},"get",[31,815,64],{"class":63},[31,817,764],{"class":45},[31,819,68],{"class":49},[31,821,764],{"class":45},[31,823,824],{"class":63},"))\n",[31,826,827,829,831,833,835,838],{"class":33,"line":173},[31,828,60],{"class":59},[31,830,64],{"class":63},[31,832,808],{"class":67},[31,834,71],{"class":63},[31,836,837],{"class":67},"keys",[31,839,77],{"class":63},[31,841,842,844,846,848,850,853],{"class":33,"line":342},[31,843,60],{"class":59},[31,845,64],{"class":63},[31,847,808],{"class":67},[31,849,71],{"class":63},[31,851,852],{"class":67},"items",[31,854,77],{"class":63},[14,856,186],{},[21,858,860],{"className":23,"code":859,"language":25,"meta":26,"style":26},"Sam\ndict_keys(['name', 'age'])\ndict_items([('name', 20), ('age', 20)])\n",[28,861,862,867,894],{"__ignoreMap":26},[31,863,864],{"class":33,"line":34},[31,865,866],{"class":37},"Sam\n",[31,868,869,872,875,878,880,882,884,887,889,891],{"class":33,"line":56},[31,870,871],{"class":67},"dict_keys",[31,873,874],{"class":63},"([",[31,876,877],{"class":45},"'",[31,879,68],{"class":49},[31,881,877],{"class":45},[31,883,283],{"class":63},[31,885,886],{"class":45}," '",[31,888,785],{"class":49},[31,890,877],{"class":45},[31,892,893],{"class":63},"])\n",[31,895,896,899,902,904,906,908,910,912,915,918,920,922,924,926,928],{"class":33,"line":166},[31,897,898],{"class":67},"dict_items",[31,900,901],{"class":63},"([(",[31,903,877],{"class":45},[31,905,68],{"class":49},[31,907,877],{"class":45},[31,909,283],{"class":63},[31,911,792],{"class":279},[31,913,914],{"class":63},"),",[31,916,917],{"class":63}," (",[31,919,877],{"class":45},[31,921,785],{"class":49},[31,923,877],{"class":45},[31,925,283],{"class":63},[31,927,792],{"class":279},[31,929,930],{"class":63},")])\n",[14,932,933],{},"These methods work because each type provides its own set of methods.",[96,935,937],{"id":936},"how-to-read-method-calls","How to read method calls",[14,939,940],{},"When you see code like this:",[21,942,944],{"className":23,"code":943,"language":25,"meta":26,"style":26},"name = \"maria\"\nprint(name.upper())\n",[28,945,946,959],{"__ignoreMap":26},[31,947,948,950,952,954,957],{"class":33,"line":34},[31,949,38],{"class":37},[31,951,42],{"class":41},[31,953,46],{"class":45},[31,955,956],{"class":49},"maria",[31,958,53],{"class":45},[31,960,961,963,965,967,969,971],{"class":33,"line":56},[31,962,60],{"class":59},[31,964,64],{"class":63},[31,966,68],{"class":67},[31,968,71],{"class":63},[31,970,74],{"class":67},[31,972,77],{"class":63},[14,974,975],{},"You can read it like this:",[107,977,978,982,986],{},[110,979,980,206],{},[28,981,68],{},[110,983,984,211],{},[28,985,74],{},[110,987,988,990],{},[28,989,216],{}," calls the method",[14,992,993],{},"Some methods also take arguments.",[14,995,126],{},[21,997,999],{"className":23,"code":998,"language":25,"meta":26,"style":26},"text = \"banana\"\nnew_text = text.replace(\"a\", \"o\")\n\nprint(new_text)\n",[28,1000,1001,1014,1047,1051],{"__ignoreMap":26},[31,1002,1003,1005,1007,1009,1012],{"class":33,"line":34},[31,1004,136],{"class":37},[31,1006,42],{"class":41},[31,1008,46],{"class":45},[31,1010,1011],{"class":49},"banana",[31,1013,53],{"class":45},[31,1015,1016,1019,1021,1023,1025,1028,1030,1032,1034,1036,1038,1040,1043,1045],{"class":33,"line":56},[31,1017,1018],{"class":37},"new_text ",[31,1020,42],{"class":41},[31,1022,155],{"class":37},[31,1024,71],{"class":63},[31,1026,1027],{"class":67},"replace",[31,1029,64],{"class":63},[31,1031,764],{"class":45},[31,1033,229],{"class":49},[31,1035,764],{"class":45},[31,1037,283],{"class":63},[31,1039,46],{"class":45},[31,1041,1042],{"class":49},"o",[31,1044,764],{"class":45},[31,1046,183],{"class":63},[31,1048,1049],{"class":33,"line":166},[31,1050,170],{"emptyLinePlaceholder":169},[31,1052,1053,1055,1057,1060],{"class":33,"line":173},[31,1054,60],{"class":59},[31,1056,64],{"class":63},[31,1058,1059],{"class":67},"new_text",[31,1061,183],{"class":63},[14,1063,186],{},[21,1065,1067],{"className":23,"code":1066,"language":25,"meta":26,"style":26},"bonono\n",[28,1068,1069],{"__ignoreMap":26},[31,1070,1071],{"class":33,"line":34},[31,1072,1066],{"class":37},[14,1074,198],{},[107,1076,1077,1081,1086],{},[110,1078,1079,206],{},[28,1080,205],{},[110,1082,1083,1085],{},[28,1084,1027],{}," is the method",[110,1087,1088,1091,1092,1095],{},[28,1089,1090],{},"\"a\""," and ",[28,1093,1094],{},"\"o\""," are arguments passed into the method",[14,1097,1098],{},"A good way to read it is:",[1100,1101,1102],"blockquote",{},[14,1103,1104,1105,1107,1108,1110],{},"“Call the ",[28,1106,1027],{}," method on ",[28,1109,205],{},".”",[96,1112,1114],{"id":1113},"methods-in-classes","Methods in classes",[14,1116,1117],{},"You can also create your own methods by defining them inside a class.",[14,1119,1120,1121,1091,1125,71],{},"If you have not learned this yet, see ",[229,1122,1124],{"href":1123},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F","Python classes and objects explained",[229,1126,1128],{"href":1127},"\u002Fglossary\u002Fwhat-is-a-class-in-python\u002F","what a class is in Python",[14,1130,126],{},[21,1132,1134],{"className":23,"code":1133,"language":25,"meta":26,"style":26},"class Dog:\n    def bark(self):\n        return \"Woof!\"\n\nmy_dog = Dog()\nprint(my_dog.bark())\n",[28,1135,1136,1149,1167,1180,1184,1195],{"__ignoreMap":26},[31,1137,1138,1142,1146],{"class":33,"line":34},[31,1139,1141],{"class":1140},"sbsja","class",[31,1143,1145],{"class":1144},"sbgvK"," Dog",[31,1147,1148],{"class":63},":\n",[31,1150,1151,1154,1158,1160,1164],{"class":33,"line":56},[31,1152,1153],{"class":1140},"    def",[31,1155,1157],{"class":1156},"sGLFI"," bark",[31,1159,64],{"class":63},[31,1161,1163],{"class":1162},"smCYv","self",[31,1165,1166],{"class":63},"):\n",[31,1168,1169,1173,1175,1178],{"class":33,"line":166},[31,1170,1172],{"class":1171},"sVHd0","        return",[31,1174,46],{"class":45},[31,1176,1177],{"class":49},"Woof!",[31,1179,53],{"class":45},[31,1181,1182],{"class":33,"line":173},[31,1183,170],{"emptyLinePlaceholder":169},[31,1185,1186,1189,1191,1193],{"class":33,"line":342},[31,1187,1188],{"class":37},"my_dog ",[31,1190,42],{"class":41},[31,1192,1145],{"class":67},[31,1194,163],{"class":63},[31,1196,1197,1199,1201,1204,1206,1209],{"class":33,"line":347},[31,1198,60],{"class":59},[31,1200,64],{"class":63},[31,1202,1203],{"class":67},"my_dog",[31,1205,71],{"class":63},[31,1207,1208],{"class":67},"bark",[31,1210,77],{"class":63},[14,1212,186],{},[21,1214,1216],{"className":23,"code":1215,"language":25,"meta":26,"style":26},"Woof!\n",[28,1217,1218],{"__ignoreMap":26},[31,1219,1220],{"class":33,"line":34},[31,1221,1215],{"class":37},[14,1223,392],{},[107,1225,1226,1232,1237],{},[110,1227,1228,1231],{},[28,1229,1230],{},"Dog"," is a class",[110,1233,1234,1236],{},[28,1235,1203],{}," is an object made from that class",[110,1238,1239,1242,1243,1245],{},[28,1240,1241],{},"bark()"," is a method of the ",[28,1244,1230],{}," class",[429,1247,1249,1250,1252],{"id":1248},"what-self-means","What ",[28,1251,1163],{}," means",[14,1254,1255,1256,1258],{},"A method in a class usually has ",[28,1257,1163],{}," as its first parameter:",[21,1260,1262],{"className":23,"code":1261,"language":25,"meta":26,"style":26},"class Person:\n    def greet(self):\n        return \"Hello\"\n\np = Person()\nprint(p.greet())\n",[28,1263,1264,1273,1286,1297,1301,1312],{"__ignoreMap":26},[31,1265,1266,1268,1271],{"class":33,"line":34},[31,1267,1141],{"class":1140},[31,1269,1270],{"class":1144}," Person",[31,1272,1148],{"class":63},[31,1274,1275,1277,1280,1282,1284],{"class":33,"line":56},[31,1276,1153],{"class":1140},[31,1278,1279],{"class":1156}," greet",[31,1281,64],{"class":63},[31,1283,1163],{"class":1162},[31,1285,1166],{"class":63},[31,1287,1288,1290,1292,1295],{"class":33,"line":166},[31,1289,1172],{"class":1171},[31,1291,46],{"class":45},[31,1293,1294],{"class":49},"Hello",[31,1296,53],{"class":45},[31,1298,1299],{"class":33,"line":173},[31,1300,170],{"emptyLinePlaceholder":169},[31,1302,1303,1306,1308,1310],{"class":33,"line":342},[31,1304,1305],{"class":37},"p ",[31,1307,42],{"class":41},[31,1309,1270],{"class":67},[31,1311,163],{"class":63},[31,1313,1314,1316,1318,1320,1322,1325],{"class":33,"line":347},[31,1315,60],{"class":59},[31,1317,64],{"class":63},[31,1319,14],{"class":67},[31,1321,71],{"class":63},[31,1323,1324],{"class":67},"greet",[31,1326,77],{"class":63},[14,1328,186],{},[21,1330,1332],{"className":23,"code":1331,"language":25,"meta":26,"style":26},"Hello\n",[28,1333,1334],{"__ignoreMap":26},[31,1335,1336],{"class":33,"line":34},[31,1337,1331],{"class":37},[14,1339,1340,1342],{},[28,1341,1163],{}," refers to the current object.",[14,1344,1345,1346,1348],{},"You do not pass ",[28,1347,1163],{}," yourself when calling the method. Python does that for you.",[14,1350,1351],{},"Here is another example:",[21,1353,1355],{"className":23,"code":1354,"language":25,"meta":26,"style":26},"class Counter:\n    def __init__(self):\n        self.value = 0\n\n    def increase(self):\n        self.value += 1\n\ncounter = Counter()\ncounter.increase()\ncounter.increase()\n\nprint(counter.value)\n",[28,1356,1357,1366,1379,1396,1400,1413,1427,1431,1442,1455,1466,1471],{"__ignoreMap":26},[31,1358,1359,1361,1364],{"class":33,"line":34},[31,1360,1141],{"class":1140},[31,1362,1363],{"class":1144}," Counter",[31,1365,1148],{"class":63},[31,1367,1368,1370,1373,1375,1377],{"class":33,"line":56},[31,1369,1153],{"class":1140},[31,1371,1372],{"class":59}," __init__",[31,1374,64],{"class":63},[31,1376,1163],{"class":1162},[31,1378,1166],{"class":63},[31,1380,1381,1384,1386,1390,1393],{"class":33,"line":166},[31,1382,1383],{"class":536},"        self",[31,1385,71],{"class":63},[31,1387,1389],{"class":1388},"skxfh","value",[31,1391,1392],{"class":41}," =",[31,1394,1395],{"class":279}," 0\n",[31,1397,1398],{"class":33,"line":173},[31,1399,170],{"emptyLinePlaceholder":169},[31,1401,1402,1404,1407,1409,1411],{"class":33,"line":342},[31,1403,1153],{"class":1140},[31,1405,1406],{"class":1156}," increase",[31,1408,64],{"class":63},[31,1410,1163],{"class":1162},[31,1412,1166],{"class":63},[31,1414,1415,1417,1419,1421,1424],{"class":33,"line":347},[31,1416,1383],{"class":536},[31,1418,71],{"class":63},[31,1420,1389],{"class":1388},[31,1422,1423],{"class":41}," +=",[31,1425,1426],{"class":279}," 1\n",[31,1428,1429],{"class":33,"line":648},[31,1430,170],{"emptyLinePlaceholder":169},[31,1432,1433,1436,1438,1440],{"class":33,"line":664},[31,1434,1435],{"class":37},"counter ",[31,1437,42],{"class":41},[31,1439,1363],{"class":67},[31,1441,163],{"class":63},[31,1443,1445,1448,1450,1453],{"class":33,"line":1444},9,[31,1446,1447],{"class":37},"counter",[31,1449,71],{"class":63},[31,1451,1452],{"class":67},"increase",[31,1454,163],{"class":63},[31,1456,1458,1460,1462,1464],{"class":33,"line":1457},10,[31,1459,1447],{"class":37},[31,1461,71],{"class":63},[31,1463,1452],{"class":67},[31,1465,163],{"class":63},[31,1467,1469],{"class":33,"line":1468},11,[31,1470,170],{"emptyLinePlaceholder":169},[31,1472,1474,1476,1478,1480,1482,1484],{"class":33,"line":1473},12,[31,1475,60],{"class":59},[31,1477,64],{"class":63},[31,1479,1447],{"class":67},[31,1481,71],{"class":63},[31,1483,1389],{"class":1388},[31,1485,183],{"class":63},[14,1487,186],{},[21,1489,1491],{"className":23,"code":1490,"language":25,"meta":26,"style":26},"2\n",[28,1492,1493],{"__ignoreMap":26},[31,1494,1495],{"class":33,"line":34},[31,1496,1490],{"class":279},[14,1498,220,1499,1502],{},[28,1500,1501],{},"increase()"," method changes data stored in the object.",[96,1504,1506],{"id":1505},"what-beginners-often-confuse","What beginners often confuse",[14,1508,1509],{},"Beginners often run into the same problems when learning methods.",[429,1511,1513],{"id":1512},"forgetting-parentheses","Forgetting parentheses",[14,1515,1516],{},"A method call usually needs parentheses.",[14,1518,126],{},[21,1520,1522],{"className":23,"code":1521,"language":25,"meta":26,"style":26},"text = \"hello\"\n\nprint(text.upper)    # no parentheses\nprint(text.upper())  # correct\n",[28,1523,1524,1537,1541,1558],{"__ignoreMap":26},[31,1525,1526,1528,1530,1532,1535],{"class":33,"line":34},[31,1527,136],{"class":37},[31,1529,42],{"class":41},[31,1531,46],{"class":45},[31,1533,1534],{"class":49},"hello",[31,1536,53],{"class":45},[31,1538,1539],{"class":33,"line":56},[31,1540,170],{"emptyLinePlaceholder":169},[31,1542,1543,1545,1547,1549,1551,1553,1555],{"class":33,"line":166},[31,1544,60],{"class":59},[31,1546,64],{"class":63},[31,1548,205],{"class":67},[31,1550,71],{"class":63},[31,1552,74],{"class":1388},[31,1554,336],{"class":63},[31,1556,1557],{"class":318},"    # no parentheses\n",[31,1559,1560,1562,1564,1566,1568,1570,1573],{"class":33,"line":173},[31,1561,60],{"class":59},[31,1563,64],{"class":63},[31,1565,205],{"class":67},[31,1567,71],{"class":63},[31,1569,74],{"class":67},[31,1571,1572],{"class":63},"())",[31,1574,1575],{"class":318},"  # correct\n",[14,1577,186],{},[21,1579,1581],{"className":23,"code":1580,"language":25,"meta":26,"style":26},"\u003Cbuilt-in method upper of str object at ...>\nHELLO\n",[28,1582,1583,1613],{"__ignoreMap":26},[31,1584,1585,1588,1591,1594,1597,1601,1604,1607,1610],{"class":33,"line":34},[31,1586,1587],{"class":41},"\u003C",[31,1589,1590],{"class":37},"built",[31,1592,1593],{"class":41},"-in",[31,1595,1596],{"class":37}," method upper of ",[31,1598,1600],{"class":1599},"sZMiF","str",[31,1602,1603],{"class":1599}," object",[31,1605,1606],{"class":37}," at ",[31,1608,1609],{"class":536},"...",[31,1611,1612],{"class":41},">\n",[31,1614,1615],{"class":33,"line":56},[31,1616,1617],{"class":536},"HELLO\n",[14,1619,1620],{},"Without parentheses, you are referring to the method itself, not calling it.",[429,1622,1624],{"id":1623},"using-the-wrong-method-on-the-wrong-type","Using the wrong method on the wrong type",[14,1626,1627],{},"Not all types have the same methods.",[14,1629,126],{},[21,1631,1633],{"className":23,"code":1632,"language":25,"meta":26,"style":26},"numbers = [1, 2, 3]\nprint(numbers.lower())\n",[28,1634,1635,1655],{"__ignoreMap":26},[31,1636,1637,1639,1641,1643,1645,1647,1649,1651,1653],{"class":33,"line":34},[31,1638,271],{"class":37},[31,1640,42],{"class":41},[31,1642,276],{"class":63},[31,1644,280],{"class":279},[31,1646,283],{"class":63},[31,1648,286],{"class":279},[31,1650,283],{"class":63},[31,1652,291],{"class":279},[31,1654,294],{"class":63},[31,1656,1657,1659,1661,1663,1665,1667],{"class":33,"line":56},[31,1658,60],{"class":59},[31,1660,64],{"class":63},[31,1662,312],{"class":67},[31,1664,71],{"class":63},[31,1666,489],{"class":67},[31,1668,77],{"class":63},[14,1670,1671,1672,1674],{},"This causes an error because lists do not have a ",[28,1673,442],{}," method.",[14,1676,1677,1678,1681,1682,71],{},"You would get an ",[28,1679,1680],{},"AttributeError",". If you see that problem, read ",[229,1683,1685,1686],{"href":1684},"\u002Ferrors\u002Fattributeerror-object-has-no-attribute-fix\u002F","how to fix ",[28,1687,1688],{},"AttributeError: object has no attribute",[429,1690,1692],{"id":1691},"confusing-functions-with-methods","Confusing functions with methods",[14,1694,1695],{},"These are different:",[21,1697,1699],{"className":23,"code":1698,"language":25,"meta":26,"style":26},"text = \"hello\"\n\nprint(len(text))    # function\nprint(text.upper()) # method\n",[28,1700,1701,1713,1717,1734],{"__ignoreMap":26},[31,1702,1703,1705,1707,1709,1711],{"class":33,"line":34},[31,1704,136],{"class":37},[31,1706,42],{"class":41},[31,1708,46],{"class":45},[31,1710,1534],{"class":49},[31,1712,53],{"class":45},[31,1714,1715],{"class":33,"line":56},[31,1716,170],{"emptyLinePlaceholder":169},[31,1718,1719,1721,1723,1725,1727,1729,1731],{"class":33,"line":166},[31,1720,60],{"class":59},[31,1722,64],{"class":63},[31,1724,307],{"class":59},[31,1726,64],{"class":63},[31,1728,205],{"class":67},[31,1730,315],{"class":63},[31,1732,1733],{"class":318},"    # function\n",[31,1735,1736,1738,1740,1742,1744,1746,1748],{"class":33,"line":173},[31,1737,60],{"class":59},[31,1739,64],{"class":63},[31,1741,205],{"class":67},[31,1743,71],{"class":63},[31,1745,74],{"class":67},[31,1747,1572],{"class":63},[31,1749,1750],{"class":318}," # method\n",[14,1752,1753],{},"Both are valid, but they are called differently.",[429,1755,1757],{"id":1756},"not-understanding-that-methods-belong-to-objects","Not understanding that methods belong to objects",[14,1759,1760],{},"If you are not sure what kind of object you have, these tools help:",[21,1762,1764],{"className":23,"code":1763,"language":25,"meta":26,"style":26},"value = \"hello\"\n\nprint(type(value))\nprint(dir(value))\nhelp(value.upper)\n",[28,1765,1766,1779,1783,1798,1813],{"__ignoreMap":26},[31,1767,1768,1771,1773,1775,1777],{"class":33,"line":34},[31,1769,1770],{"class":37},"value ",[31,1772,42],{"class":41},[31,1774,46],{"class":45},[31,1776,1534],{"class":49},[31,1778,53],{"class":45},[31,1780,1781],{"class":33,"line":56},[31,1782,170],{"emptyLinePlaceholder":169},[31,1784,1785,1787,1789,1792,1794,1796],{"class":33,"line":166},[31,1786,60],{"class":59},[31,1788,64],{"class":63},[31,1790,1791],{"class":1599},"type",[31,1793,64],{"class":63},[31,1795,1389],{"class":67},[31,1797,824],{"class":63},[31,1799,1800,1802,1804,1807,1809,1811],{"class":33,"line":173},[31,1801,60],{"class":59},[31,1803,64],{"class":63},[31,1805,1806],{"class":59},"dir",[31,1808,64],{"class":63},[31,1810,1389],{"class":67},[31,1812,824],{"class":63},[31,1814,1815,1818,1820,1822,1824,1826],{"class":33,"line":342},[31,1816,1817],{"class":59},"help",[31,1819,64],{"class":63},[31,1821,1389],{"class":67},[31,1823,71],{"class":63},[31,1825,74],{"class":1388},[31,1827,183],{"class":63},[14,1829,1830],{},"Useful debugging commands:",[107,1832,1833,1839,1845],{},[110,1834,1835,1838],{},[28,1836,1837],{},"type(value)"," shows the type",[110,1840,1841,1844],{},[28,1842,1843],{},"dir(value)"," shows available attributes and methods",[110,1846,1847,1850],{},[28,1848,1849],{},"help(value.method_name)"," shows documentation for a specific method",[96,1852,1854],{"id":1853},"faq","FAQ",[429,1856,1858],{"id":1857},"is-a-method-the-same-as-a-function-in-python","Is a method the same as a function in Python?",[14,1860,1861],{},"Not exactly. A method is a function attached to an object or class.",[429,1863,1865],{"id":1864},"how-do-i-know-if-something-is-a-method","How do I know if something is a method?",[14,1867,1868,1869,1872],{},"If you call it on an object with dot notation, like ",[28,1870,1871],{},"text.lower()",", it is a method.",[429,1874,1876],{"id":1875},"why-does-python-use-both-functions-and-methods","Why does Python use both functions and methods?",[14,1878,1879],{},"Some actions make more sense as object behavior, while others work as general-purpose functions.",[429,1881,1883],{"id":1882},"what-does-self-mean-in-a-method","What does self mean in a method?",[14,1885,1886,1888],{},[28,1887,1163],{}," refers to the current object and is used inside class methods.",[429,1890,1892],{"id":1891},"can-i-create-my-own-methods","Can I create my own methods?",[14,1894,1895],{},"Yes. When you define functions inside a class, they become methods of that class.",[96,1897,1899],{"id":1898},"see-also","See also",[107,1901,1902,1906,1911,1916,1920,1924],{},[110,1903,1904],{},[229,1905,1124],{"href":1123},[110,1907,1908],{},[229,1909,1910],{"href":231},"What is an object in Python?",[110,1912,1913],{},[229,1914,1915],{"href":1127},"What is a class in Python?",[110,1917,1918],{},[229,1919,420],{"href":419},[110,1921,1922],{},[229,1923,549],{"href":548},[110,1925,1926],{},[229,1927,1928,1929],{"href":1684},"How to fix ",[28,1930,1688],{},[1932,1933,1934],"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 .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 .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":26,"searchDepth":56,"depth":56,"links":1936},[1937,1938,1939,1944,1945,1949,1955,1962],{"id":98,"depth":56,"text":99},{"id":235,"depth":56,"text":236},{"id":423,"depth":56,"text":424,"children":1940},[1941,1942,1943],{"id":431,"depth":166,"text":432},{"id":552,"depth":166,"text":553},{"id":723,"depth":166,"text":724},{"id":936,"depth":56,"text":937},{"id":1113,"depth":56,"text":1114,"children":1946},[1947],{"id":1248,"depth":166,"text":1948},"What self means",{"id":1505,"depth":56,"text":1506,"children":1950},[1951,1952,1953,1954],{"id":1512,"depth":166,"text":1513},{"id":1623,"depth":166,"text":1624},{"id":1691,"depth":166,"text":1692},{"id":1756,"depth":166,"text":1757},{"id":1853,"depth":56,"text":1854,"children":1956},[1957,1958,1959,1960,1961],{"id":1857,"depth":166,"text":1858},{"id":1864,"depth":166,"text":1865},{"id":1875,"depth":166,"text":1876},{"id":1882,"depth":166,"text":1883},{"id":1891,"depth":166,"text":1892},{"id":1898,"depth":56,"text":1899},"Master what is a method in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-a-method-in-python",{"title":5,"description":1963},"glossary\u002Fwhat-is-a-method-in-python","uFw1fQX0mgEuhu-MTxahLKIdwLt-kkHxY9WlorKjuqU",1777585495736]