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