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