[{"data":1,"prerenderedAt":1667},["ShallowReactive",2],{"doc-\u002Flearn\u002Fobject-oriented-programming-in-python-explained":3},{"id":4,"title":5,"body":6,"description":1660,"extension":1661,"meta":1662,"navigation":138,"path":1663,"seo":1664,"stem":1665,"__hash__":1666},"content\u002Flearn\u002Fobject-oriented-programming-in-python-explained.md","Object-Oriented Programming in Python Explained",{"type":7,"value":8,"toc":1638},"minimark",[9,13,30,33,51,54,59,227,232,241,244,279,282,286,289,292,319,322,328,332,335,338,355,358,362,365,368,451,455,491,494,514,527,531,537,543,546,678,682,697,700,717,723,735,742,747,750,835,839,848,851,875,881,891,895,898,922,925,948,951,955,958,972,975,1173,1177,1205,1208,1211,1273,1277,1286,1289,1293,1296,1299,1315,1318,1342,1346,1349,1368,1371,1408,1414,1453,1456,1515,1518,1544,1548,1553,1556,1560,1563,1567,1570,1577,1582,1586,1589,1593,1628,1634],[10,11,5],"h1",{"id":12},"object-oriented-programming-in-python-explained",[14,15,16,17,21,22,25,26,29],"p",{},"Object-oriented programming, usually called ",[18,19,20],"strong",{},"OOP",", is a way to organize Python code using ",[18,23,24],{},"classes"," and ",[18,27,28],{},"objects",".",[14,31,32],{},"For beginners, the main idea is simple:",[34,35,36,44],"ul",{},[37,38,39,40,43],"li",{},"A ",[18,41,42],{},"class"," describes what something should look like and do",[37,45,46,47,50],{},"An ",[18,48,49],{},"object"," is one real thing created from that class",[14,52,53],{},"OOP helps you keep related data and behavior together. It is useful in bigger programs, and you will see it often in real Python code, libraries, and tutorials.",[55,56,58],"h2",{"id":57},"a-quick-example","A quick example",[60,61,66],"pre",{"className":62,"code":63,"language":64,"meta":65,"style":65},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name, 'says woof')\n\ndog = Dog('Max')\ndog.bark()\n","python","",[67,68,69,85,112,133,140,155,185,190,213],"code",{"__ignoreMap":65},[70,71,74,77,81],"span",{"class":72,"line":73},"line",1,[70,75,42],{"class":76},"sbsja",[70,78,80],{"class":79},"sbgvK"," Dog",[70,82,84],{"class":83},"sP7_E",":\n",[70,86,88,91,95,98,102,105,109],{"class":72,"line":87},2,[70,89,90],{"class":76},"    def",[70,92,94],{"class":93},"sptTA"," __init__",[70,96,97],{"class":83},"(",[70,99,101],{"class":100},"smCYv","self",[70,103,104],{"class":83},",",[70,106,108],{"class":107},"sFwrP"," name",[70,110,111],{"class":83},"):\n",[70,113,115,119,121,125,129],{"class":72,"line":114},3,[70,116,118],{"class":117},"s_hVV","        self",[70,120,29],{"class":83},[70,122,124],{"class":123},"skxfh","name",[70,126,128],{"class":127},"smGrS"," =",[70,130,132],{"class":131},"su5hD"," name\n",[70,134,136],{"class":72,"line":135},4,[70,137,139],{"emptyLinePlaceholder":138},true,"\n",[70,141,143,145,149,151,153],{"class":72,"line":142},5,[70,144,90],{"class":76},[70,146,148],{"class":147},"sGLFI"," bark",[70,150,97],{"class":83},[70,152,101],{"class":100},[70,154,111],{"class":83},[70,156,158,161,163,165,167,169,171,175,179,182],{"class":72,"line":157},6,[70,159,160],{"class":93},"        print",[70,162,97],{"class":83},[70,164,101],{"class":117},[70,166,29],{"class":83},[70,168,124],{"class":123},[70,170,104],{"class":83},[70,172,174],{"class":173},"sjJ54"," '",[70,176,178],{"class":177},"s_sjI","says woof",[70,180,181],{"class":173},"'",[70,183,184],{"class":83},")\n",[70,186,188],{"class":72,"line":187},7,[70,189,139],{"emptyLinePlaceholder":138},[70,191,193,196,199,202,204,206,209,211],{"class":72,"line":192},8,[70,194,195],{"class":131},"dog ",[70,197,198],{"class":127},"=",[70,200,80],{"class":201},"slqww",[70,203,97],{"class":83},[70,205,181],{"class":173},[70,207,208],{"class":177},"Max",[70,210,181],{"class":173},[70,212,184],{"class":83},[70,214,216,219,221,224],{"class":72,"line":215},9,[70,217,218],{"class":131},"dog",[70,220,29],{"class":83},[70,222,223],{"class":201},"bark",[70,225,226],{"class":83},"()\n",[14,228,229],{},[18,230,231],{},"Output:",[60,233,235],{"className":62,"code":234,"language":64,"meta":65,"style":65},"Max says woof\n",[67,236,237],{"__ignoreMap":65},[70,238,239],{"class":72,"line":73},[70,240,234],{"class":131},[14,242,243],{},"What this shows:",[34,245,246,254,262,271],{},[37,247,248,251,252],{},[67,249,250],{},"Dog"," is a ",[18,253,42],{},[37,255,256,259,260],{},[67,257,258],{},"dog = Dog('Max')"," creates an ",[18,261,49],{},[37,263,264,267,268],{},[67,265,266],{},"self.name"," is an ",[18,269,270],{},"attribute",[37,272,273,251,276],{},[67,274,275],{},"bark()",[18,277,278],{},"method",[14,280,281],{},"This is the core OOP idea: a class defines behavior, and an object is a real instance created from that class.",[55,283,285],{"id":284},"what-object-oriented-programming-means","What object-oriented programming means",[14,287,288],{},"OOP is a way to organize code using classes and objects.",[14,290,291],{},"In Python:",[34,293,294,299,304,307,313],{},[37,295,39,296,298],{},[18,297,42],{}," is a blueprint",[37,300,46,301,303],{},[18,302,49],{}," is a value created from that blueprint",[37,305,306],{},"Objects can store data and perform actions",[37,308,309,310],{},"Object data is usually stored in ",[18,311,312],{},"attributes",[37,314,315,316],{},"Object actions are usually written as ",[18,317,318],{},"methods",[14,320,321],{},"This lets you group related pieces of code together instead of keeping everything separate.",[14,323,324,325,327],{},"For example, a ",[67,326,250],{}," object can store its name and also know how to bark. That is often easier to understand than managing names and dog functions in different places.",[55,329,331],{"id":330},"why-beginners-should-learn-oop","Why beginners should learn OOP",[14,333,334],{},"Beginners do not need OOP for every program, but it is important to understand the basic idea.",[14,336,337],{},"OOP helps because it:",[34,339,340,343,346,349,352],{},[37,341,342],{},"Groups related data and behavior together",[37,344,345],{},"Makes larger programs easier to read",[37,347,348],{},"Helps model real things like users, files, or game characters",[37,350,351],{},"Appears often in real-world Python code",[37,353,354],{},"Makes many libraries easier to understand",[14,356,357],{},"If you are still learning variables, functions, and lists, that is fine. You do not need to master OOP right away. But once you know the basics, learning OOP will help you read and write more structured code.",[55,359,361],{"id":360},"classes-and-objects","Classes and objects",[14,363,364],{},"A class defines what objects of that type should have.",[14,366,367],{},"An object is created by calling the class name.",[60,369,371],{"className":62,"code":370,"language":64,"meta":65,"style":65},"class Dog:\n    pass\n\ndog1 = Dog()\ndog2 = Dog()\n\nprint(type(dog1))\nprint(type(dog2))\n",[67,372,373,381,387,391,402,413,417,436],{"__ignoreMap":65},[70,374,375,377,379],{"class":72,"line":73},[70,376,42],{"class":76},[70,378,80],{"class":79},[70,380,84],{"class":83},[70,382,383],{"class":72,"line":87},[70,384,386],{"class":385},"sVHd0","    pass\n",[70,388,389],{"class":72,"line":114},[70,390,139],{"emptyLinePlaceholder":138},[70,392,393,396,398,400],{"class":72,"line":135},[70,394,395],{"class":131},"dog1 ",[70,397,198],{"class":127},[70,399,80],{"class":201},[70,401,226],{"class":83},[70,403,404,407,409,411],{"class":72,"line":142},[70,405,406],{"class":131},"dog2 ",[70,408,198],{"class":127},[70,410,80],{"class":201},[70,412,226],{"class":83},[70,414,415],{"class":72,"line":157},[70,416,139],{"emptyLinePlaceholder":138},[70,418,419,422,424,428,430,433],{"class":72,"line":187},[70,420,421],{"class":93},"print",[70,423,97],{"class":83},[70,425,427],{"class":426},"sZMiF","type",[70,429,97],{"class":83},[70,431,432],{"class":201},"dog1",[70,434,435],{"class":83},"))\n",[70,437,438,440,442,444,446,449],{"class":72,"line":192},[70,439,421],{"class":93},[70,441,97],{"class":83},[70,443,427],{"class":426},[70,445,97],{"class":83},[70,447,448],{"class":201},"dog2",[70,450,435],{"class":83},[14,452,453],{},[18,454,231],{},[60,456,458],{"className":62,"code":457,"language":64,"meta":65,"style":65},"\u003Cclass '__main__.Dog'>\n\u003Cclass '__main__.Dog'>\n",[67,459,460,477],{"__ignoreMap":65},[70,461,462,465,467,469,472,474],{"class":72,"line":73},[70,463,464],{"class":127},"\u003C",[70,466,42],{"class":76},[70,468,174],{"class":173},[70,470,471],{"class":177},"__main__.Dog",[70,473,181],{"class":173},[70,475,476],{"class":127},">\n",[70,478,479,481,483,485,487,489],{"class":72,"line":87},[70,480,464],{"class":127},[70,482,42],{"class":76},[70,484,174],{"class":173},[70,486,471],{"class":177},[70,488,181],{"class":173},[70,490,476],{"class":127},[14,492,493],{},"Important points:",[34,495,496,501,508,511],{},[37,497,498,500],{},[67,499,250],{}," is the class",[37,502,503,25,505,507],{},[67,504,432],{},[67,506,448],{}," are objects",[37,509,510],{},"Multiple objects can come from the same class",[37,512,513],{},"Each object can later have its own data",[14,515,516,517,522,523,29],{},"If you want a full beginner explanation, see ",[518,519,521],"a",{"href":520},"\u002Flearn\u002Fpython-classes-and-objects-explained\u002F","Python classes and objects explained"," or the glossary page on ",[518,524,526],{"href":525},"\u002Fglossary\u002Fwhat-is-a-class-in-python\u002F","what a class is in Python",[55,528,530],{"id":529},"attributes-and-methods","Attributes and methods",[14,532,533,536],{},[18,534,535],{},"Attributes"," are values stored on an object.",[14,538,539,542],{},[18,540,541],{},"Methods"," are functions defined inside a class.",[14,544,545],{},"Here is a simple example:",[60,547,549],{"className":62,"code":548,"language":64,"meta":65,"style":65},"class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def bark(self):\n        print(self.name, 'says woof')\n\ndog = Dog('Bella')\n\nprint(dog.name)\ndog.bark()\n",[67,550,551,559,575,587,591,603,625,629,648,652,667],{"__ignoreMap":65},[70,552,553,555,557],{"class":72,"line":73},[70,554,42],{"class":76},[70,556,80],{"class":79},[70,558,84],{"class":83},[70,560,561,563,565,567,569,571,573],{"class":72,"line":87},[70,562,90],{"class":76},[70,564,94],{"class":93},[70,566,97],{"class":83},[70,568,101],{"class":100},[70,570,104],{"class":83},[70,572,108],{"class":107},[70,574,111],{"class":83},[70,576,577,579,581,583,585],{"class":72,"line":114},[70,578,118],{"class":117},[70,580,29],{"class":83},[70,582,124],{"class":123},[70,584,128],{"class":127},[70,586,132],{"class":131},[70,588,589],{"class":72,"line":135},[70,590,139],{"emptyLinePlaceholder":138},[70,592,593,595,597,599,601],{"class":72,"line":142},[70,594,90],{"class":76},[70,596,148],{"class":147},[70,598,97],{"class":83},[70,600,101],{"class":100},[70,602,111],{"class":83},[70,604,605,607,609,611,613,615,617,619,621,623],{"class":72,"line":157},[70,606,160],{"class":93},[70,608,97],{"class":83},[70,610,101],{"class":117},[70,612,29],{"class":83},[70,614,124],{"class":123},[70,616,104],{"class":83},[70,618,174],{"class":173},[70,620,178],{"class":177},[70,622,181],{"class":173},[70,624,184],{"class":83},[70,626,627],{"class":72,"line":187},[70,628,139],{"emptyLinePlaceholder":138},[70,630,631,633,635,637,639,641,644,646],{"class":72,"line":192},[70,632,195],{"class":131},[70,634,198],{"class":127},[70,636,80],{"class":201},[70,638,97],{"class":83},[70,640,181],{"class":173},[70,642,643],{"class":177},"Bella",[70,645,181],{"class":173},[70,647,184],{"class":83},[70,649,650],{"class":72,"line":215},[70,651,139],{"emptyLinePlaceholder":138},[70,653,655,657,659,661,663,665],{"class":72,"line":654},10,[70,656,421],{"class":93},[70,658,97],{"class":83},[70,660,218],{"class":201},[70,662,29],{"class":83},[70,664,124],{"class":123},[70,666,184],{"class":83},[70,668,670,672,674,676],{"class":72,"line":669},11,[70,671,218],{"class":131},[70,673,29],{"class":83},[70,675,223],{"class":201},[70,677,226],{"class":83},[14,679,680],{},[18,681,231],{},[60,683,685],{"className":62,"code":684,"language":64,"meta":65,"style":65},"Bella\nBella says woof\n",[67,686,687,692],{"__ignoreMap":65},[70,688,689],{"class":72,"line":73},[70,690,691],{"class":131},"Bella\n",[70,693,694],{"class":72,"line":87},[70,695,696],{"class":131},"Bella says woof\n",[14,698,699],{},"In this example:",[34,701,702,707,712],{},[37,703,704,706],{},[67,705,124],{}," is an attribute",[37,708,709,711],{},[67,710,275],{}," is a method",[37,713,714,715],{},"The method uses the object's own data with ",[67,716,266],{},[14,718,719,720,722],{},"The ",[67,721,101],{}," parameter refers to the current object. It lets a method work with that object's attributes and other methods.",[14,724,725,726,730,731,29],{},"If you want more detail, read ",[518,727,729],{"href":728},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained\u002F","basic methods in Python classes explained"," and the glossary page on ",[518,732,734],{"href":733},"\u002Fglossary\u002Fwhat-is-a-method-in-python\u002F","what a method is in Python",[55,736,738,739],{"id":737},"the-role-of-__init__","The role of ",[67,740,741],{},"__init__",[14,743,744,746],{},[67,745,741],{}," runs when a new object is created.",[14,748,749],{},"It is commonly used to set starting attribute values.",[60,751,753],{"className":62,"code":752,"language":64,"meta":65,"style":65},"class User:\n    def __init__(self, username):\n        self.username = username\n\nuser = User(\"alice\")\nprint(user.username)\n",[67,754,755,764,781,795,799,820],{"__ignoreMap":65},[70,756,757,759,762],{"class":72,"line":73},[70,758,42],{"class":76},[70,760,761],{"class":79}," User",[70,763,84],{"class":83},[70,765,766,768,770,772,774,776,779],{"class":72,"line":87},[70,767,90],{"class":76},[70,769,94],{"class":93},[70,771,97],{"class":83},[70,773,101],{"class":100},[70,775,104],{"class":83},[70,777,778],{"class":107}," username",[70,780,111],{"class":83},[70,782,783,785,787,790,792],{"class":72,"line":114},[70,784,118],{"class":117},[70,786,29],{"class":83},[70,788,789],{"class":123},"username",[70,791,128],{"class":127},[70,793,794],{"class":131}," username\n",[70,796,797],{"class":72,"line":135},[70,798,139],{"emptyLinePlaceholder":138},[70,800,801,804,806,808,810,813,816,818],{"class":72,"line":142},[70,802,803],{"class":131},"user ",[70,805,198],{"class":127},[70,807,761],{"class":201},[70,809,97],{"class":83},[70,811,812],{"class":173},"\"",[70,814,815],{"class":177},"alice",[70,817,812],{"class":173},[70,819,184],{"class":83},[70,821,822,824,826,829,831,833],{"class":72,"line":157},[70,823,421],{"class":93},[70,825,97],{"class":83},[70,827,828],{"class":201},"user",[70,830,29],{"class":83},[70,832,789],{"class":123},[70,834,184],{"class":83},[14,836,837],{},[18,838,231],{},[60,840,842],{"className":62,"code":841,"language":64,"meta":65,"style":65},"alice\n",[67,843,844],{"__ignoreMap":65},[70,845,846],{"class":72,"line":73},[70,847,841],{"class":131},[14,849,850],{},"What happens here:",[34,852,853,860,865],{},[37,854,855,856,859],{},"Python creates a new ",[67,857,858],{},"User"," object",[37,861,862,864],{},[67,863,741],{}," runs automatically",[37,866,867,868,871,872],{},"The value ",[67,869,870],{},"\"alice\""," is stored in ",[67,873,874],{},"self.username",[14,876,877,878,880],{},"Beginners can think of ",[67,879,741],{}," as the setup method for a class.",[14,882,883,884,29],{},"For a full guide, see ",[518,885,887,888,890],{"href":886},"\u002Flearn\u002Fthe-__init__-method-in-python-explained","the ",[67,889,741],{}," method in Python explained",[55,892,894],{"id":893},"a-simple-mental-model","A simple mental model",[14,896,897],{},"A useful way to think about OOP is this:",[34,899,900,906,912,917],{},[37,901,902,905],{},[18,903,904],{},"Class"," = recipe or template",[37,907,908,911],{},[18,909,910],{},"Object"," = thing made from the recipe",[37,913,914,916],{},[18,915,535],{}," = data about the thing",[37,918,919,921],{},[18,920,541],{}," = actions the thing can do",[14,923,924],{},"Example:",[34,926,927,932,938,943],{},[37,928,929,930],{},"Class: ",[67,931,250],{},[37,933,934,935],{},"Object: ",[67,936,937],{},"Dog(\"Max\")",[37,939,940,941],{},"Attribute: ",[67,942,124],{},[37,944,945,946],{},"Method: ",[67,947,275],{},[14,949,950],{},"This mental model is not perfect for every situation, but it is very helpful when you are starting.",[55,952,954],{"id":953},"when-to-use-oop-and-when-not-to","When to use OOP and when not to",[14,956,957],{},"Use OOP when:",[34,959,960,963,966,969],{},[37,961,962],{},"Related data and functions belong together",[37,964,965],{},"You need many similar objects",[37,967,968],{},"You are modeling things like users, accounts, products, or game characters",[37,970,971],{},"You want code to be easier to organize as a program grows",[14,973,974],{},"Simple example: if you have many dogs, each with its own name, age, and behavior, a class can make sense.",[60,976,978],{"className":62,"code":977,"language":64,"meta":65,"style":65},"class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def describe(self):\n        print(self.name, \"is\", self.age, \"years old\")\n\ndog1 = Dog(\"Max\", 3)\ndog2 = Dog(\"Luna\", 5)\n\ndog1.describe()\ndog2.describe()\n",[67,979,980,988,1009,1021,1035,1039,1052,1094,1098,1122,1146,1150,1162],{"__ignoreMap":65},[70,981,982,984,986],{"class":72,"line":73},[70,983,42],{"class":76},[70,985,80],{"class":79},[70,987,84],{"class":83},[70,989,990,992,994,996,998,1000,1002,1004,1007],{"class":72,"line":87},[70,991,90],{"class":76},[70,993,94],{"class":93},[70,995,97],{"class":83},[70,997,101],{"class":100},[70,999,104],{"class":83},[70,1001,108],{"class":107},[70,1003,104],{"class":83},[70,1005,1006],{"class":107}," age",[70,1008,111],{"class":83},[70,1010,1011,1013,1015,1017,1019],{"class":72,"line":114},[70,1012,118],{"class":117},[70,1014,29],{"class":83},[70,1016,124],{"class":123},[70,1018,128],{"class":127},[70,1020,132],{"class":131},[70,1022,1023,1025,1027,1030,1032],{"class":72,"line":135},[70,1024,118],{"class":117},[70,1026,29],{"class":83},[70,1028,1029],{"class":123},"age",[70,1031,128],{"class":127},[70,1033,1034],{"class":131}," age\n",[70,1036,1037],{"class":72,"line":142},[70,1038,139],{"emptyLinePlaceholder":138},[70,1040,1041,1043,1046,1048,1050],{"class":72,"line":157},[70,1042,90],{"class":76},[70,1044,1045],{"class":147}," describe",[70,1047,97],{"class":83},[70,1049,101],{"class":100},[70,1051,111],{"class":83},[70,1053,1054,1056,1058,1060,1062,1064,1066,1069,1072,1074,1076,1079,1081,1083,1085,1087,1090,1092],{"class":72,"line":187},[70,1055,160],{"class":93},[70,1057,97],{"class":83},[70,1059,101],{"class":117},[70,1061,29],{"class":83},[70,1063,124],{"class":123},[70,1065,104],{"class":83},[70,1067,1068],{"class":173}," \"",[70,1070,1071],{"class":177},"is",[70,1073,812],{"class":173},[70,1075,104],{"class":83},[70,1077,1078],{"class":117}," self",[70,1080,29],{"class":83},[70,1082,1029],{"class":123},[70,1084,104],{"class":83},[70,1086,1068],{"class":173},[70,1088,1089],{"class":177},"years old",[70,1091,812],{"class":173},[70,1093,184],{"class":83},[70,1095,1096],{"class":72,"line":192},[70,1097,139],{"emptyLinePlaceholder":138},[70,1099,1100,1102,1104,1106,1108,1110,1112,1114,1116,1120],{"class":72,"line":215},[70,1101,395],{"class":131},[70,1103,198],{"class":127},[70,1105,80],{"class":201},[70,1107,97],{"class":83},[70,1109,812],{"class":173},[70,1111,208],{"class":177},[70,1113,812],{"class":173},[70,1115,104],{"class":83},[70,1117,1119],{"class":1118},"srdBf"," 3",[70,1121,184],{"class":83},[70,1123,1124,1126,1128,1130,1132,1134,1137,1139,1141,1144],{"class":72,"line":654},[70,1125,406],{"class":131},[70,1127,198],{"class":127},[70,1129,80],{"class":201},[70,1131,97],{"class":83},[70,1133,812],{"class":173},[70,1135,1136],{"class":177},"Luna",[70,1138,812],{"class":173},[70,1140,104],{"class":83},[70,1142,1143],{"class":1118}," 5",[70,1145,184],{"class":83},[70,1147,1148],{"class":72,"line":669},[70,1149,139],{"emptyLinePlaceholder":138},[70,1151,1153,1155,1157,1160],{"class":72,"line":1152},12,[70,1154,432],{"class":131},[70,1156,29],{"class":83},[70,1158,1159],{"class":201},"describe",[70,1161,226],{"class":83},[70,1163,1165,1167,1169,1171],{"class":72,"line":1164},13,[70,1166,448],{"class":131},[70,1168,29],{"class":83},[70,1170,1159],{"class":201},[70,1172,226],{"class":83},[14,1174,1175],{},[18,1176,231],{},[60,1178,1180],{"className":62,"code":1179,"language":64,"meta":65,"style":65},"Max is 3 years old\nLuna is 5 years old\n",[67,1181,1182,1194],{"__ignoreMap":65},[70,1183,1184,1187,1189,1191],{"class":72,"line":73},[70,1185,1186],{"class":131},"Max ",[70,1188,1071],{"class":127},[70,1190,1119],{"class":1118},[70,1192,1193],{"class":131}," years old\n",[70,1195,1196,1199,1201,1203],{"class":72,"line":87},[70,1197,1198],{"class":131},"Luna ",[70,1200,1071],{"class":127},[70,1202,1143],{"class":1118},[70,1204,1193],{"class":131},[14,1206,1207],{},"Do not force OOP into very small scripts.",[14,1209,1210],{},"For example, if you only need to add two numbers or read one file once, a simple function is often better.",[60,1212,1214],{"className":62,"code":1213,"language":64,"meta":65,"style":65},"def add_numbers(a, b):\n    return a + b\n\nprint(add_numbers(2, 3))\n",[67,1215,1216,1235,1249,1253],{"__ignoreMap":65},[70,1217,1218,1221,1224,1226,1228,1230,1233],{"class":72,"line":73},[70,1219,1220],{"class":76},"def",[70,1222,1223],{"class":147}," add_numbers",[70,1225,97],{"class":83},[70,1227,518],{"class":107},[70,1229,104],{"class":83},[70,1231,1232],{"class":107}," b",[70,1234,111],{"class":83},[70,1236,1237,1240,1243,1246],{"class":72,"line":87},[70,1238,1239],{"class":385},"    return",[70,1241,1242],{"class":131}," a ",[70,1244,1245],{"class":127},"+",[70,1247,1248],{"class":131}," b\n",[70,1250,1251],{"class":72,"line":114},[70,1252,139],{"emptyLinePlaceholder":138},[70,1254,1255,1257,1259,1262,1264,1267,1269,1271],{"class":72,"line":135},[70,1256,421],{"class":93},[70,1258,97],{"class":83},[70,1260,1261],{"class":201},"add_numbers",[70,1263,97],{"class":83},[70,1265,1266],{"class":1118},"2",[70,1268,104],{"class":83},[70,1270,1119],{"class":1118},[70,1272,435],{"class":83},[14,1274,1275],{},[18,1276,231],{},[60,1278,1280],{"className":62,"code":1279,"language":64,"meta":65,"style":65},"5\n",[67,1281,1282],{"__ignoreMap":65},[70,1283,1284],{"class":72,"line":73},[70,1285,1279],{"class":1118},[14,1287,1288],{},"OOP is a tool. It is useful in the right situation, but not required for every Python program.",[55,1290,1292],{"id":1291},"what-this-page-does-not-cover-in-depth","What this page does not cover in depth",[14,1294,1295],{},"This page is a concept overview, not a full class reference.",[14,1297,1298],{},"It does not explain these topics in detail:",[34,1300,1301,1304,1309,1312],{},[37,1302,1303],{},"Full class syntax",[37,1305,1306,1307],{},"All details of ",[67,1308,741],{},[37,1310,1311],{},"Inheritance",[37,1313,1314],{},"The difference between instance variables and class variables",[14,1316,1317],{},"For the next step, use these focused pages:",[34,1319,1320,1324,1330,1336],{},[37,1321,1322],{},[518,1323,521],{"href":520},[37,1325,1326],{},[518,1327,887,1328,890],{"href":886},[67,1329,741],{},[37,1331,1332],{},[518,1333,1335],{"href":1334},"\u002Flearn\u002Finstance-vs-class-variables-in-python\u002F","instance vs class variables in Python",[37,1337,1338],{},[518,1339,1341],{"href":1340},"\u002Fhow-to\u002Fhow-to-inherit-from-a-class-in-python\u002F","how to inherit from a class in Python",[55,1343,1345],{"id":1344},"common-mistakes","Common mistakes",[14,1347,1348],{},"These are common beginner mistakes when learning OOP:",[34,1350,1351,1354,1357,1362,1365],{},[37,1352,1353],{},"Thinking OOP is required for all Python programs",[37,1355,1356],{},"Confusing a class with an object",[37,1358,1359,1360],{},"Forgetting that methods need ",[67,1361,101],{},[37,1363,1364],{},"Treating attributes and methods as the same thing",[37,1366,1367],{},"Using classes before understanding functions and variables",[14,1369,1370],{},"Here is a common method mistake:",[60,1372,1374],{"className":62,"code":1373,"language":64,"meta":65,"style":65},"class Dog:\n    def bark():\n        print(\"woof\")\n",[67,1375,1376,1384,1393],{"__ignoreMap":65},[70,1377,1378,1380,1382],{"class":72,"line":73},[70,1379,42],{"class":76},[70,1381,80],{"class":79},[70,1383,84],{"class":83},[70,1385,1386,1388,1390],{"class":72,"line":87},[70,1387,90],{"class":76},[70,1389,148],{"class":147},[70,1391,1392],{"class":83},"():\n",[70,1394,1395,1397,1399,1401,1404,1406],{"class":72,"line":114},[70,1396,160],{"class":93},[70,1398,97],{"class":83},[70,1400,812],{"class":173},[70,1402,1403],{"class":177},"woof",[70,1405,812],{"class":173},[70,1407,184],{"class":83},[14,1409,1410,1411,1413],{},"This method is missing ",[67,1412,101],{},". In most beginner class methods, you need:",[60,1415,1417],{"className":62,"code":1416,"language":64,"meta":65,"style":65},"class Dog:\n    def bark(self):\n        print(\"woof\")\n",[67,1418,1419,1427,1439],{"__ignoreMap":65},[70,1420,1421,1423,1425],{"class":72,"line":73},[70,1422,42],{"class":76},[70,1424,80],{"class":79},[70,1426,84],{"class":83},[70,1428,1429,1431,1433,1435,1437],{"class":72,"line":87},[70,1430,90],{"class":76},[70,1432,148],{"class":147},[70,1434,97],{"class":83},[70,1436,101],{"class":100},[70,1438,111],{"class":83},[70,1440,1441,1443,1445,1447,1449,1451],{"class":72,"line":114},[70,1442,160],{"class":93},[70,1444,97],{"class":83},[70,1446,812],{"class":173},[70,1448,1403],{"class":177},[70,1450,812],{"class":173},[70,1452,184],{"class":83},[14,1454,1455],{},"If you want to inspect an object while learning, these commands can help:",[60,1457,1459],{"className":62,"code":1458,"language":64,"meta":65,"style":65},"print(type(dog))\nprint(dog.name)\nprint(dir(dog))\nhelp(Dog)\n",[67,1460,1461,1475,1489,1504],{"__ignoreMap":65},[70,1462,1463,1465,1467,1469,1471,1473],{"class":72,"line":73},[70,1464,421],{"class":93},[70,1466,97],{"class":83},[70,1468,427],{"class":426},[70,1470,97],{"class":83},[70,1472,218],{"class":201},[70,1474,435],{"class":83},[70,1476,1477,1479,1481,1483,1485,1487],{"class":72,"line":87},[70,1478,421],{"class":93},[70,1480,97],{"class":83},[70,1482,218],{"class":201},[70,1484,29],{"class":83},[70,1486,124],{"class":123},[70,1488,184],{"class":83},[70,1490,1491,1493,1495,1498,1500,1502],{"class":72,"line":114},[70,1492,421],{"class":93},[70,1494,97],{"class":83},[70,1496,1497],{"class":93},"dir",[70,1499,97],{"class":83},[70,1501,218],{"class":201},[70,1503,435],{"class":83},[70,1505,1506,1509,1511,1513],{"class":72,"line":135},[70,1507,1508],{"class":93},"help",[70,1510,97],{"class":83},[70,1512,250],{"class":201},[70,1514,184],{"class":83},[14,1516,1517],{},"What these do:",[34,1519,1520,1526,1532,1538],{},[37,1521,1522,1525],{},[67,1523,1524],{},"type(dog)"," shows the object's type",[37,1527,1528,1531],{},[67,1529,1530],{},"dog.name"," shows one attribute value",[37,1533,1534,1537],{},[67,1535,1536],{},"dir(dog)"," lists available attributes and methods",[37,1539,1540,1543],{},[67,1541,1542],{},"help(Dog)"," shows built-in help for the class",[55,1545,1547],{"id":1546},"faq","FAQ",[1549,1550,1552],"h3",{"id":1551},"what-is-object-oriented-programming-in-simple-words","What is object-oriented programming in simple words?",[14,1554,1555],{},"It is a way to organize code by creating classes and objects that keep related data and actions together.",[1549,1557,1559],{"id":1558},"what-is-the-difference-between-a-class-and-an-object","What is the difference between a class and an object?",[14,1561,1562],{},"A class is a blueprint. An object is a real instance created from that blueprint.",[1549,1564,1566],{"id":1565},"do-beginners-need-oop-right-away","Do beginners need OOP right away?",[14,1568,1569],{},"Not at the start, but beginners should learn the basic idea after functions, variables, and data types.",[1549,1571,1573,1574,1576],{"id":1572},"what-is-self-in-a-python-class","What is ",[67,1575,101],{}," in a Python class?",[14,1578,1579,1581],{},[67,1580,101],{}," refers to the current object, so methods can access that object's attributes and other methods.",[1549,1583,1585],{"id":1584},"is-oop-better-than-functions","Is OOP better than functions?",[14,1587,1588],{},"Not always. OOP is helpful for organizing related data and behavior, but simple functions are often enough for small scripts.",[55,1590,1592],{"id":1591},"see-also","See also",[34,1594,1595,1599,1605,1610,1616,1622],{},[37,1596,1597],{},[518,1598,521],{"href":520},[37,1600,1601],{},[518,1602,719,1603,890],{"href":886},[67,1604,741],{},[37,1606,1607],{},[518,1608,1609],{"href":728},"Basic methods in Python classes explained",[37,1611,1612],{},[518,1613,1615],{"href":1614},"\u002Fhow-to\u002Fhow-to-create-a-class-in-python\u002F","How to create a class in Python",[37,1617,1618],{},[518,1619,1621],{"href":1620},"\u002Fhow-to\u002Fhow-to-create-an-object-in-python\u002F","How to create an object in Python",[37,1623,1624],{},[518,1625,1627],{"href":1626},"\u002Fglossary\u002Fwhat-is-an-object-in-python\u002F","What is an object in Python?",[14,1629,1630,1631,1633],{},"Next, learn the practical building blocks: classes, objects, ",[67,1632,741],{},", and methods.",[1635,1636,1637],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .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 .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 .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}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":65,"searchDepth":87,"depth":87,"links":1639},[1640,1641,1642,1643,1644,1645,1647,1648,1649,1650,1651,1659],{"id":57,"depth":87,"text":58},{"id":284,"depth":87,"text":285},{"id":330,"depth":87,"text":331},{"id":360,"depth":87,"text":361},{"id":529,"depth":87,"text":530},{"id":737,"depth":87,"text":1646},"The role of __init__",{"id":893,"depth":87,"text":894},{"id":953,"depth":87,"text":954},{"id":1291,"depth":87,"text":1292},{"id":1344,"depth":87,"text":1345},{"id":1546,"depth":87,"text":1547,"children":1652},[1653,1654,1655,1656,1658],{"id":1551,"depth":114,"text":1552},{"id":1558,"depth":114,"text":1559},{"id":1565,"depth":114,"text":1566},{"id":1572,"depth":114,"text":1657},"What is self in a Python class?",{"id":1584,"depth":114,"text":1585},{"id":1591,"depth":87,"text":1592},"Master object oriented programming in python explained in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained",{"title":5,"description":1660},"learn\u002Fobject-oriented-programming-in-python-explained","P_yUnKejR4SEauR0Jk0gA5gonb7-gUX2iE11Q2c7uH0",1777585498979]