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