[{"data":1,"prerenderedAt":1547},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-inheritance-in-python":3},{"id":4,"title":5,"body":6,"description":1540,"extension":1541,"meta":1542,"navigation":99,"path":1543,"seo":1544,"stem":1545,"__hash__":1546},"content\u002Fglossary\u002Fwhat-is-inheritance-in-python.md","What Is Inheritance in Python?",{"type":7,"value":8,"toc":1519},"minimark",[9,13,17,26,29,164,170,179,194,199,202,228,231,250,262,266,269,272,286,304,308,311,366,369,386,389,409,416,420,423,437,440,565,569,583,597,601,604,726,730,744,747,777,780,784,790,793,926,930,944,955,963,968,982,985,1170,1174,1189,1195,1218,1221,1225,1228,1231,1246,1249,1257,1260,1264,1267,1285,1296,1300,1303,1317,1320,1405,1408,1444,1448,1453,1456,1460,1463,1467,1470,1474,1477,1481,1484,1488,1512,1515],[10,11,5],"h1",{"id":12},"what-is-inheritance-in-python",[14,15,16],"p",{},"Inheritance in Python is a way for one class to use code from another class.",[14,18,19,20,25],{},"This helps you reuse methods and attributes instead of writing the same code again. It is a common idea in object-oriented programming, so it makes more sense if you already understand ",[21,22,24],"a",{"href":23},"\u002Flearn\u002Fpython-classes-and-objects-explained","classes and objects in Python",".",[14,27,28],{},"Here is the basic idea:",[30,31,36],"pre",{"className":32,"code":33,"language":34,"meta":35,"style":35},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    pass\n\npet = Dog()\nprint(pet.speak())\n","python","",[37,38,39,56,76,94,101,116,122,127,144],"code",{"__ignoreMap":35},[40,41,44,48,52],"span",{"class":42,"line":43},"line",1,[40,45,47],{"class":46},"sbsja","class",[40,49,51],{"class":50},"sbgvK"," Animal",[40,53,55],{"class":54},"sP7_E",":\n",[40,57,59,62,66,69,73],{"class":42,"line":58},2,[40,60,61],{"class":46},"    def",[40,63,65],{"class":64},"sGLFI"," speak",[40,67,68],{"class":54},"(",[40,70,72],{"class":71},"smCYv","self",[40,74,75],{"class":54},"):\n",[40,77,79,83,87,91],{"class":42,"line":78},3,[40,80,82],{"class":81},"sVHd0","        return",[40,84,86],{"class":85},"sjJ54"," \"",[40,88,90],{"class":89},"s_sjI","Some sound",[40,92,93],{"class":85},"\"\n",[40,95,97],{"class":42,"line":96},4,[40,98,100],{"emptyLinePlaceholder":99},true,"\n",[40,102,104,106,109,111,114],{"class":42,"line":103},5,[40,105,47],{"class":46},[40,107,108],{"class":50}," Dog",[40,110,68],{"class":54},[40,112,113],{"class":50},"Animal",[40,115,75],{"class":54},[40,117,119],{"class":42,"line":118},6,[40,120,121],{"class":81},"    pass\n",[40,123,125],{"class":42,"line":124},7,[40,126,100],{"emptyLinePlaceholder":99},[40,128,130,134,138,141],{"class":42,"line":129},8,[40,131,133],{"class":132},"su5hD","pet ",[40,135,137],{"class":136},"smGrS","=",[40,139,108],{"class":140},"slqww",[40,142,143],{"class":54},"()\n",[40,145,147,151,153,156,158,161],{"class":42,"line":146},9,[40,148,150],{"class":149},"sptTA","print",[40,152,68],{"class":54},[40,154,155],{"class":140},"pet",[40,157,25],{"class":54},[40,159,160],{"class":140},"speak",[40,162,163],{"class":54},"())\n",[14,165,166],{},[167,168,169],"strong",{},"Output:",[30,171,173],{"className":32,"code":172,"language":34,"meta":35,"style":35},"Some sound\n",[37,174,175],{"__ignoreMap":35},[40,176,177],{"class":42,"line":43},[40,178,172],{"class":132},[14,180,181,184,185,188,189,191,192,25],{},[37,182,183],{},"Dog"," does not define ",[37,186,187],{},"speak()",", but it can still use it because ",[37,190,183],{}," inherits from ",[37,193,113],{},[195,196,198],"h2",{"id":197},"what-inheritance-means","What inheritance means",[14,200,201],{},"Inheritance lets one class use code from another class.",[203,204,205,216,225],"ul",{},[206,207,208,209,212,213],"li",{},"The new class is often called the ",[167,210,211],{},"child class"," or ",[167,214,215],{},"subclass",[206,217,218,219,212,222],{},"The class it inherits from is often called the ",[167,220,221],{},"parent class",[167,223,224],{},"base class",[206,226,227],{},"It helps avoid repeating the same methods and attributes",[14,229,230],{},"In simple terms:",[203,232,233,238,243],{},[206,234,235,237],{},[37,236,113],{}," is the parent class",[206,239,240,242],{},[37,241,183],{}," is the child class",[206,244,245,247,248],{},[37,246,183],{}," can use behavior defined in ",[37,249,113],{},[14,251,252,253,257,258,25],{},"If you are new to this topic, it also helps to know ",[21,254,256],{"href":255},"\u002Fglossary\u002Fwhat-is-a-class-in-python","what a class is in Python"," and ",[21,259,261],{"href":260},"\u002Fglossary\u002Fwhat-is-an-object-in-python","what an object is in Python",[195,263,265],{"id":264},"why-beginners-use-inheritance","Why beginners use inheritance",[14,267,268],{},"Beginners usually use inheritance for simple code reuse.",[14,270,271],{},"It is useful when:",[203,273,274,277,280,283],{},[206,275,276],{},"You want to reuse code instead of writing the same method again",[206,278,279],{},"You have related classes that share common behavior",[206,281,282],{},"You want to keep code organized when objects are similar",[206,284,285],{},"You want updates in shared code to affect multiple child classes",[14,287,288,289,257,291,294,295,212,298,301,302,25],{},"For example, if both ",[37,290,183],{},[37,292,293],{},"Cat"," are animals, they may both need methods like ",[37,296,297],{},"eat()",[37,299,300],{},"sleep()",". Instead of writing those methods twice, you can put them in ",[37,303,113],{},[195,305,307],{"id":306},"basic-inheritance-syntax","Basic inheritance syntax",[14,309,310],{},"To create a child class, put the parent class name in parentheses after the child class name.",[30,312,314],{"className":32,"code":313,"language":34,"meta":35,"style":35},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    pass\n",[37,315,316,324,336,346,350,362],{"__ignoreMap":35},[40,317,318,320,322],{"class":42,"line":43},[40,319,47],{"class":46},[40,321,51],{"class":50},[40,323,55],{"class":54},[40,325,326,328,330,332,334],{"class":42,"line":58},[40,327,61],{"class":46},[40,329,65],{"class":64},[40,331,68],{"class":54},[40,333,72],{"class":71},[40,335,75],{"class":54},[40,337,338,340,342,344],{"class":42,"line":78},[40,339,82],{"class":81},[40,341,86],{"class":85},[40,343,90],{"class":89},[40,345,93],{"class":85},[40,347,348],{"class":42,"line":96},[40,349,100],{"emptyLinePlaceholder":99},[40,351,352,354,356,358,360],{"class":42,"line":103},[40,353,47],{"class":46},[40,355,108],{"class":50},[40,357,68],{"class":54},[40,359,113],{"class":50},[40,361,75],{"class":54},[40,363,364],{"class":42,"line":118},[40,365,121],{"class":81},[14,367,368],{},"This line is the key part:",[30,370,372],{"className":32,"code":371,"language":34,"meta":35,"style":35},"class Dog(Animal):\n",[37,373,374],{"__ignoreMap":35},[40,375,376,378,380,382,384],{"class":42,"line":43},[40,377,47],{"class":46},[40,379,108],{"class":50},[40,381,68],{"class":54},[40,383,113],{"class":50},[40,385,75],{"class":54},[14,387,388],{},"It means:",[203,390,391,397,404],{},[206,392,393,191,395],{},[37,394,183],{},[37,396,113],{},[206,398,399,401,402],{},[37,400,183],{}," gets access to methods and attributes from ",[37,403,113],{},[206,405,406,408],{},[37,407,183],{}," can also define its own methods",[14,410,411,412,25],{},"If you want a refresher on class methods, see ",[21,413,415],{"href":414},"\u002Flearn\u002Fbasic-methods-in-python-classes-explained","basic methods in Python classes",[195,417,419],{"id":418},"what-a-child-class-can-do","What a child class can do",[14,421,422],{},"A child class can do several useful things:",[203,424,425,428,431,434],{},[206,426,427],{},"Use parent methods without rewriting them",[206,429,430],{},"Add new methods that only belong to the child class",[206,432,433],{},"Override a parent method with a new version",[206,435,436],{},"Call parent behavior when needed",[14,438,439],{},"Here is a simple example:",[30,441,443],{"className":32,"code":442,"language":34,"meta":35,"style":35},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def fetch(self):\n        return \"The dog runs after the ball\"\n\npet = Dog()\n\nprint(pet.speak())\nprint(pet.fetch())\n",[37,444,445,453,465,475,479,491,504,515,519,529,534,549],{"__ignoreMap":35},[40,446,447,449,451],{"class":42,"line":43},[40,448,47],{"class":46},[40,450,51],{"class":50},[40,452,55],{"class":54},[40,454,455,457,459,461,463],{"class":42,"line":58},[40,456,61],{"class":46},[40,458,65],{"class":64},[40,460,68],{"class":54},[40,462,72],{"class":71},[40,464,75],{"class":54},[40,466,467,469,471,473],{"class":42,"line":78},[40,468,82],{"class":81},[40,470,86],{"class":85},[40,472,90],{"class":89},[40,474,93],{"class":85},[40,476,477],{"class":42,"line":96},[40,478,100],{"emptyLinePlaceholder":99},[40,480,481,483,485,487,489],{"class":42,"line":103},[40,482,47],{"class":46},[40,484,108],{"class":50},[40,486,68],{"class":54},[40,488,113],{"class":50},[40,490,75],{"class":54},[40,492,493,495,498,500,502],{"class":42,"line":118},[40,494,61],{"class":46},[40,496,497],{"class":64}," fetch",[40,499,68],{"class":54},[40,501,72],{"class":71},[40,503,75],{"class":54},[40,505,506,508,510,513],{"class":42,"line":124},[40,507,82],{"class":81},[40,509,86],{"class":85},[40,511,512],{"class":89},"The dog runs after the ball",[40,514,93],{"class":85},[40,516,517],{"class":42,"line":129},[40,518,100],{"emptyLinePlaceholder":99},[40,520,521,523,525,527],{"class":42,"line":146},[40,522,133],{"class":132},[40,524,137],{"class":136},[40,526,108],{"class":140},[40,528,143],{"class":54},[40,530,532],{"class":42,"line":531},10,[40,533,100],{"emptyLinePlaceholder":99},[40,535,537,539,541,543,545,547],{"class":42,"line":536},11,[40,538,150],{"class":149},[40,540,68],{"class":54},[40,542,155],{"class":140},[40,544,25],{"class":54},[40,546,160],{"class":140},[40,548,163],{"class":54},[40,550,552,554,556,558,560,563],{"class":42,"line":551},12,[40,553,150],{"class":149},[40,555,68],{"class":54},[40,557,155],{"class":140},[40,559,25],{"class":54},[40,561,562],{"class":140},"fetch",[40,564,163],{"class":54},[14,566,567],{},[167,568,169],{},[30,570,572],{"className":32,"code":571,"language":34,"meta":35,"style":35},"Some sound\nThe dog runs after the ball\n",[37,573,574,578],{"__ignoreMap":35},[40,575,576],{"class":42,"line":43},[40,577,172],{"class":132},[40,579,580],{"class":42,"line":58},[40,581,582],{"class":132},"The dog runs after the ball\n",[14,584,585,587,588,590,591,593,594,25],{},[37,586,183],{}," inherited ",[37,589,187],{}," from ",[37,592,113],{}," and added its own method, ",[37,595,596],{},"fetch()",[195,598,600],{"id":599},"simple-example-to-include","Simple example to include",[14,602,603],{},"Here is a slightly fuller example that shows both inherited behavior and new child behavior:",[30,605,607],{"className":32,"code":606,"language":34,"meta":35,"style":35},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def fetch(self):\n        return \"Fetching the stick\"\n\ndog = Dog()\n\nprint(dog.speak())\nprint(dog.fetch())\n",[37,608,609,617,629,639,643,655,667,678,682,693,697,712],{"__ignoreMap":35},[40,610,611,613,615],{"class":42,"line":43},[40,612,47],{"class":46},[40,614,51],{"class":50},[40,616,55],{"class":54},[40,618,619,621,623,625,627],{"class":42,"line":58},[40,620,61],{"class":46},[40,622,65],{"class":64},[40,624,68],{"class":54},[40,626,72],{"class":71},[40,628,75],{"class":54},[40,630,631,633,635,637],{"class":42,"line":78},[40,632,82],{"class":81},[40,634,86],{"class":85},[40,636,90],{"class":89},[40,638,93],{"class":85},[40,640,641],{"class":42,"line":96},[40,642,100],{"emptyLinePlaceholder":99},[40,644,645,647,649,651,653],{"class":42,"line":103},[40,646,47],{"class":46},[40,648,108],{"class":50},[40,650,68],{"class":54},[40,652,113],{"class":50},[40,654,75],{"class":54},[40,656,657,659,661,663,665],{"class":42,"line":118},[40,658,61],{"class":46},[40,660,497],{"class":64},[40,662,68],{"class":54},[40,664,72],{"class":71},[40,666,75],{"class":54},[40,668,669,671,673,676],{"class":42,"line":124},[40,670,82],{"class":81},[40,672,86],{"class":85},[40,674,675],{"class":89},"Fetching the stick",[40,677,93],{"class":85},[40,679,680],{"class":42,"line":129},[40,681,100],{"emptyLinePlaceholder":99},[40,683,684,687,689,691],{"class":42,"line":146},[40,685,686],{"class":132},"dog ",[40,688,137],{"class":136},[40,690,108],{"class":140},[40,692,143],{"class":54},[40,694,695],{"class":42,"line":531},[40,696,100],{"emptyLinePlaceholder":99},[40,698,699,701,703,706,708,710],{"class":42,"line":536},[40,700,150],{"class":149},[40,702,68],{"class":54},[40,704,705],{"class":140},"dog",[40,707,25],{"class":54},[40,709,160],{"class":140},[40,711,163],{"class":54},[40,713,714,716,718,720,722,724],{"class":42,"line":551},[40,715,150],{"class":149},[40,717,68],{"class":54},[40,719,705],{"class":140},[40,721,25],{"class":54},[40,723,562],{"class":140},[40,725,163],{"class":54},[14,727,728],{},[167,729,169],{},[30,731,733],{"className":32,"code":732,"language":34,"meta":35,"style":35},"Some sound\nFetching the stick\n",[37,734,735,739],{"__ignoreMap":35},[40,736,737],{"class":42,"line":43},[40,738,172],{"class":132},[40,740,741],{"class":42,"line":58},[40,742,743],{"class":132},"Fetching the stick\n",[14,745,746],{},"What happens here:",[203,748,749,756,762,770],{},[206,750,751,753,754],{},[37,752,113],{}," defines ",[37,755,187],{},[206,757,758,191,760],{},[37,759,183],{},[37,761,113],{},[206,763,764,766,767,769],{},[37,765,183],{}," does not need to write ",[37,768,187],{}," again",[206,771,772,774,775],{},[37,773,183],{}," adds a new method called ",[37,776,596],{},[14,778,779],{},"This is a good example of how inheritance reduces repetition.",[195,781,783],{"id":782},"method-overriding","Method overriding",[14,785,786,787,25],{},"A child class can replace a parent method with its own version. This is called ",[167,788,789],{},"overriding",[14,791,792],{},"Use overriding when classes share a general idea but need different behavior.",[30,794,796],{"className":32,"code":795,"language":34,"meta":35,"style":35},"class Animal:\n    def speak(self):\n        return \"Some sound\"\n\nclass Dog(Animal):\n    def speak(self):\n        return \"Woof\"\n\nanimal = Animal()\ndog = Dog()\n\nprint(animal.speak())\nprint(dog.speak())\n",[37,797,798,806,818,828,832,844,856,867,871,882,892,896,911],{"__ignoreMap":35},[40,799,800,802,804],{"class":42,"line":43},[40,801,47],{"class":46},[40,803,51],{"class":50},[40,805,55],{"class":54},[40,807,808,810,812,814,816],{"class":42,"line":58},[40,809,61],{"class":46},[40,811,65],{"class":64},[40,813,68],{"class":54},[40,815,72],{"class":71},[40,817,75],{"class":54},[40,819,820,822,824,826],{"class":42,"line":78},[40,821,82],{"class":81},[40,823,86],{"class":85},[40,825,90],{"class":89},[40,827,93],{"class":85},[40,829,830],{"class":42,"line":96},[40,831,100],{"emptyLinePlaceholder":99},[40,833,834,836,838,840,842],{"class":42,"line":103},[40,835,47],{"class":46},[40,837,108],{"class":50},[40,839,68],{"class":54},[40,841,113],{"class":50},[40,843,75],{"class":54},[40,845,846,848,850,852,854],{"class":42,"line":118},[40,847,61],{"class":46},[40,849,65],{"class":64},[40,851,68],{"class":54},[40,853,72],{"class":71},[40,855,75],{"class":54},[40,857,858,860,862,865],{"class":42,"line":124},[40,859,82],{"class":81},[40,861,86],{"class":85},[40,863,864],{"class":89},"Woof",[40,866,93],{"class":85},[40,868,869],{"class":42,"line":129},[40,870,100],{"emptyLinePlaceholder":99},[40,872,873,876,878,880],{"class":42,"line":146},[40,874,875],{"class":132},"animal ",[40,877,137],{"class":136},[40,879,51],{"class":140},[40,881,143],{"class":54},[40,883,884,886,888,890],{"class":42,"line":531},[40,885,686],{"class":132},[40,887,137],{"class":136},[40,889,108],{"class":140},[40,891,143],{"class":54},[40,893,894],{"class":42,"line":536},[40,895,100],{"emptyLinePlaceholder":99},[40,897,898,900,902,905,907,909],{"class":42,"line":551},[40,899,150],{"class":149},[40,901,68],{"class":54},[40,903,904],{"class":140},"animal",[40,906,25],{"class":54},[40,908,160],{"class":140},[40,910,163],{"class":54},[40,912,914,916,918,920,922,924],{"class":42,"line":913},13,[40,915,150],{"class":149},[40,917,68],{"class":54},[40,919,705],{"class":140},[40,921,25],{"class":54},[40,923,160],{"class":140},[40,925,163],{"class":54},[14,927,928],{},[167,929,169],{},[30,931,933],{"className":32,"code":932,"language":34,"meta":35,"style":35},"Some sound\nWoof\n",[37,934,935,939],{"__ignoreMap":35},[40,936,937],{"class":42,"line":43},[40,938,172],{"class":132},[40,940,941],{"class":42,"line":58},[40,942,943],{"class":132},"Woof\n",[14,945,946,947,191,949,951,952,954],{},"Even though ",[37,948,183],{},[37,950,113],{},", it uses its own ",[37,953,187],{}," method because the child version overrides the parent version.",[195,956,958,959,962],{"id":957},"using-super-at-a-beginner-level","Using ",[37,960,961],{},"super()"," at a beginner level",[14,964,965,967],{},[37,966,961],{}," lets a child class call a method from the parent class.",[14,969,970,971,974,975,25],{},"Beginners often see this in the ",[37,972,973],{},"__init__"," method, which is explained in ",[21,976,978,979,981],{"href":977},"\u002Flearn\u002Fthe-__init__-method-in-python-explained","the ",[37,980,973],{}," method in Python",[14,983,984],{},"Example:",[30,986,988],{"className":32,"code":987,"language":34,"meta":35,"style":35},"class Animal:\n    def __init__(self, name):\n        self.name = name\n\nclass Dog(Animal):\n    def __init__(self, name, breed):\n        super().__init__(name)\n        self.breed = breed\n\npet = Dog(\"Max\", \"Labrador\")\n\nprint(pet.name)\nprint(pet.breed)\n",[37,989,990,998,1018,1036,1040,1052,1073,1091,1105,1109,1138,1142,1156],{"__ignoreMap":35},[40,991,992,994,996],{"class":42,"line":43},[40,993,47],{"class":46},[40,995,51],{"class":50},[40,997,55],{"class":54},[40,999,1000,1002,1005,1007,1009,1012,1016],{"class":42,"line":58},[40,1001,61],{"class":46},[40,1003,1004],{"class":149}," __init__",[40,1006,68],{"class":54},[40,1008,72],{"class":71},[40,1010,1011],{"class":54},",",[40,1013,1015],{"class":1014},"sFwrP"," name",[40,1017,75],{"class":54},[40,1019,1020,1024,1026,1030,1033],{"class":42,"line":78},[40,1021,1023],{"class":1022},"s_hVV","        self",[40,1025,25],{"class":54},[40,1027,1029],{"class":1028},"skxfh","name",[40,1031,1032],{"class":136}," =",[40,1034,1035],{"class":132}," name\n",[40,1037,1038],{"class":42,"line":96},[40,1039,100],{"emptyLinePlaceholder":99},[40,1041,1042,1044,1046,1048,1050],{"class":42,"line":103},[40,1043,47],{"class":46},[40,1045,108],{"class":50},[40,1047,68],{"class":54},[40,1049,113],{"class":50},[40,1051,75],{"class":54},[40,1053,1054,1056,1058,1060,1062,1064,1066,1068,1071],{"class":42,"line":118},[40,1055,61],{"class":46},[40,1057,1004],{"class":149},[40,1059,68],{"class":54},[40,1061,72],{"class":71},[40,1063,1011],{"class":54},[40,1065,1015],{"class":1014},[40,1067,1011],{"class":54},[40,1069,1070],{"class":1014}," breed",[40,1072,75],{"class":54},[40,1074,1075,1079,1082,1084,1086,1088],{"class":42,"line":124},[40,1076,1078],{"class":1077},"sZMiF","        super",[40,1080,1081],{"class":54},"().",[40,1083,973],{"class":149},[40,1085,68],{"class":54},[40,1087,1029],{"class":140},[40,1089,1090],{"class":54},")\n",[40,1092,1093,1095,1097,1100,1102],{"class":42,"line":129},[40,1094,1023],{"class":1022},[40,1096,25],{"class":54},[40,1098,1099],{"class":1028},"breed",[40,1101,1032],{"class":136},[40,1103,1104],{"class":132}," breed\n",[40,1106,1107],{"class":42,"line":146},[40,1108,100],{"emptyLinePlaceholder":99},[40,1110,1111,1113,1115,1117,1119,1122,1125,1127,1129,1131,1134,1136],{"class":42,"line":531},[40,1112,133],{"class":132},[40,1114,137],{"class":136},[40,1116,108],{"class":140},[40,1118,68],{"class":54},[40,1120,1121],{"class":85},"\"",[40,1123,1124],{"class":89},"Max",[40,1126,1121],{"class":85},[40,1128,1011],{"class":54},[40,1130,86],{"class":85},[40,1132,1133],{"class":89},"Labrador",[40,1135,1121],{"class":85},[40,1137,1090],{"class":54},[40,1139,1140],{"class":42,"line":536},[40,1141,100],{"emptyLinePlaceholder":99},[40,1143,1144,1146,1148,1150,1152,1154],{"class":42,"line":551},[40,1145,150],{"class":149},[40,1147,68],{"class":54},[40,1149,155],{"class":140},[40,1151,25],{"class":54},[40,1153,1029],{"class":1028},[40,1155,1090],{"class":54},[40,1157,1158,1160,1162,1164,1166,1168],{"class":42,"line":913},[40,1159,150],{"class":149},[40,1161,68],{"class":54},[40,1163,155],{"class":140},[40,1165,25],{"class":54},[40,1167,1099],{"class":1028},[40,1169,1090],{"class":54},[14,1171,1172],{},[167,1173,169],{},[30,1175,1177],{"className":32,"code":1176,"language":34,"meta":35,"style":35},"Max\nLabrador\n",[37,1178,1179,1184],{"__ignoreMap":35},[40,1180,1181],{"class":42,"line":43},[40,1182,1183],{"class":132},"Max\n",[40,1185,1186],{"class":42,"line":58},[40,1187,1188],{"class":132},"Labrador\n",[14,1190,1191,1192,1194],{},"Why use ",[37,1193,961],{}," here?",[203,1196,1197,1206,1212],{},[206,1198,1199,1202,1203],{},[37,1200,1201],{},"Animal.__init__()"," sets ",[37,1204,1205],{},"self.name",[206,1207,1208,1211],{},[37,1209,1210],{},"Dog.__init__()"," still wants that setup to happen",[206,1213,1214,1217],{},[37,1215,1216],{},"super().__init__(name)"," runs the parent setup first",[14,1219,1220],{},"This is a common and useful pattern.",[195,1222,1224],{"id":1223},"when-inheritance-is-a-good-fit","When inheritance is a good fit",[14,1226,1227],{},"Inheritance works best when there is a clear parent-child relationship.",[14,1229,1230],{},"Good examples:",[203,1232,1233,1240],{},[206,1234,1235,1237,1238],{},[37,1236,183],{}," is an ",[37,1239,113],{},[206,1241,1242,1237,1244],{},[37,1243,293],{},[37,1245,113],{},[14,1247,1248],{},"Use inheritance when:",[203,1250,1251,1254],{},[206,1252,1253],{},"Multiple classes share the same fields or methods",[206,1255,1256],{},"The child classes are specialized versions of a more general class",[14,1258,1259],{},"Be careful not to force inheritance just to share a small amount of code. Sometimes simple separate classes are easier to read.",[195,1261,1263],{"id":1262},"common-beginner-confusion","Common beginner confusion",[14,1265,1266],{},"A few points often cause confusion:",[203,1268,1269,1276,1279,1282],{},[206,1270,1271,1272,1275],{},"Inheritance does ",[167,1273,1274],{},"not"," copy and paste code into the child class",[206,1277,1278],{},"The child class can access parent behavior because of the class relationship",[206,1280,1281],{},"A child class can have extra behavior that the parent does not have",[206,1283,1284],{},"Not every related class should use inheritance",[14,1286,1287,1288,1291,1292,25],{},"It also helps to remember that inheritance is about ",[167,1289,1290],{},"classes",", not about creating objects. If that part feels unclear, review ",[21,1293,1295],{"href":1294},"\u002Flearn\u002Fobject-oriented-programming-in-python-explained","object-oriented programming in Python",[195,1297,1299],{"id":1298},"common-mistakes","Common mistakes",[14,1301,1302],{},"Beginners often make these mistakes:",[203,1304,1305,1308,1311,1314],{},[206,1306,1307],{},"Confusing inheritance with creating an object",[206,1309,1310],{},"Thinking the child class must rewrite every parent method",[206,1312,1313],{},"Not understanding the difference between parent class and child class",[206,1315,1316],{},"Using inheritance when separate classes would be clearer",[14,1318,1319],{},"If you want to inspect what an object or class can do, these tools can help:",[30,1321,1323],{"className":32,"code":1322,"language":34,"meta":35,"style":35},"print(type(obj))\nprint(isinstance(obj, Animal))\nprint(isinstance(obj, Dog))\nprint(dir(obj))\nhelp(Dog)\n",[37,1324,1325,1342,1361,1379,1394],{"__ignoreMap":35},[40,1326,1327,1329,1331,1334,1336,1339],{"class":42,"line":43},[40,1328,150],{"class":149},[40,1330,68],{"class":54},[40,1332,1333],{"class":1077},"type",[40,1335,68],{"class":54},[40,1337,1338],{"class":140},"obj",[40,1340,1341],{"class":54},"))\n",[40,1343,1344,1346,1348,1351,1353,1355,1357,1359],{"class":42,"line":58},[40,1345,150],{"class":149},[40,1347,68],{"class":54},[40,1349,1350],{"class":149},"isinstance",[40,1352,68],{"class":54},[40,1354,1338],{"class":140},[40,1356,1011],{"class":54},[40,1358,51],{"class":140},[40,1360,1341],{"class":54},[40,1362,1363,1365,1367,1369,1371,1373,1375,1377],{"class":42,"line":78},[40,1364,150],{"class":149},[40,1366,68],{"class":54},[40,1368,1350],{"class":149},[40,1370,68],{"class":54},[40,1372,1338],{"class":140},[40,1374,1011],{"class":54},[40,1376,108],{"class":140},[40,1378,1341],{"class":54},[40,1380,1381,1383,1385,1388,1390,1392],{"class":42,"line":96},[40,1382,150],{"class":149},[40,1384,68],{"class":54},[40,1386,1387],{"class":149},"dir",[40,1389,68],{"class":54},[40,1391,1338],{"class":140},[40,1393,1341],{"class":54},[40,1395,1396,1399,1401,1403],{"class":42,"line":103},[40,1397,1398],{"class":149},"help",[40,1400,68],{"class":54},[40,1402,183],{"class":140},[40,1404,1090],{"class":54},[14,1406,1407],{},"What they do:",[203,1409,1410,1416,1424,1432,1438],{},[206,1411,1412,1415],{},[37,1413,1414],{},"type(obj)"," shows the object's class",[206,1417,1418,1421,1422],{},[37,1419,1420],{},"isinstance(obj, Animal)"," checks if the object is treated as an ",[37,1423,113],{},[206,1425,1426,1429,1430],{},[37,1427,1428],{},"isinstance(obj, Dog)"," checks if it is specifically a ",[37,1431,183],{},[206,1433,1434,1437],{},[37,1435,1436],{},"dir(obj)"," lists available attributes and methods",[206,1439,1440,1443],{},[37,1441,1442],{},"help(Dog)"," shows information about the class",[195,1445,1447],{"id":1446},"faq","FAQ",[1449,1450,1452],"h3",{"id":1451},"what-is-inheritance-in-python-in-simple-words","What is inheritance in Python in simple words?",[14,1454,1455],{},"It is a way for one class to use code from another class.",[1449,1457,1459],{"id":1458},"what-is-the-parent-class","What is the parent class?",[14,1461,1462],{},"The parent class is the class that provides methods and attributes to another class.",[1449,1464,1466],{"id":1465},"what-is-the-child-class","What is the child class?",[14,1468,1469],{},"The child class inherits from the parent class and can also add or change behavior.",[1449,1471,1473],{"id":1472},"can-a-child-class-change-a-parent-method","Can a child class change a parent method?",[14,1475,1476],{},"Yes. This is called overriding.",[1449,1478,1480],{"id":1479},"should-beginners-always-use-inheritance","Should beginners always use inheritance?",[14,1482,1483],{},"No. Use it when there is a clear shared relationship between classes.",[195,1485,1487],{"id":1486},"see-also","See also",[203,1489,1490,1495,1500,1506],{},[206,1491,1492],{},[21,1493,1494],{"href":1294},"Object-oriented programming in Python explained",[206,1496,1497],{},[21,1498,1499],{"href":23},"Python classes and objects explained",[206,1501,1502],{},[21,1503,1505],{"href":1504},"\u002Fhow-to\u002Fhow-to-inherit-from-a-class-in-python","How to inherit from a class in Python",[206,1507,1508],{},[21,1509,1511],{"href":1510},"\u002Fglossary\u002Fwhat-is-a-method-in-python","What is a method in Python?",[14,1513,1514],{},"Next, learn how classes and objects work so inheritance makes more sense in real code.",[1516,1517,1518],"style",{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .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 .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":35,"searchDepth":58,"depth":58,"links":1520},[1521,1522,1523,1524,1525,1526,1527,1529,1530,1531,1532,1539],{"id":197,"depth":58,"text":198},{"id":264,"depth":58,"text":265},{"id":306,"depth":58,"text":307},{"id":418,"depth":58,"text":419},{"id":599,"depth":58,"text":600},{"id":782,"depth":58,"text":783},{"id":957,"depth":58,"text":1528},"Using super() at a beginner level",{"id":1223,"depth":58,"text":1224},{"id":1262,"depth":58,"text":1263},{"id":1298,"depth":58,"text":1299},{"id":1446,"depth":58,"text":1447,"children":1533},[1534,1535,1536,1537,1538],{"id":1451,"depth":78,"text":1452},{"id":1458,"depth":78,"text":1459},{"id":1465,"depth":78,"text":1466},{"id":1472,"depth":78,"text":1473},{"id":1479,"depth":78,"text":1480},{"id":1486,"depth":58,"text":1487},"Master what is inheritance in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-inheritance-in-python",{"title":5,"description":1540},"glossary\u002Fwhat-is-inheritance-in-python","QOocROruvMCrkSFkvja_KuG1F8dNH-gIy8q4jV8hp14",1777585468130]