[{"data":1,"prerenderedAt":988},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-classes-instances-init-explained":3},{"id":4,"title":5,"body":6,"description":974,"difficulty":975,"extension":976,"framework":977,"frameworkSlug":52,"meta":978,"navigation":105,"order":74,"path":979,"qaPath":980,"seo":981,"stem":982,"subtopic":983,"topic":984,"topicSlug":985,"updated":986,"__hash__":987},"blog\u002Fblog\u002Fpython-classes-instances-init-explained.md","Python Classes and Instances Explained — __init__ vs __new__, self, and Attributes",{"type":7,"value":8,"toc":962},"minimark",[9,14,31,35,47,157,160,164,173,264,274,284,301,419,440,444,457,556,569,578,598,769,778,782,802,897,910,914,958],[10,11,13],"h2",{"id":12},"python-classes-explained","Python classes, explained",[15,16,17,18,22,23,26,27,30],"p",{},"Classes are the foundation of OOP in Python, and the basics — ",[19,20,21],"code",{},"self",", ",[19,24,25],{},"__init__",", the\nclass\u002Finstance attribute split — come up in nearly every interview. This guide builds the\nmental model from \"what is an instance\" up to what actually happens when you call\n",[19,28,29],{},"ClassName()",".",[10,32,34],{"id":33},"class-vs-instance","Class vs instance",[15,36,37,38,42,43,46],{},"A ",[39,40,41],"strong",{},"class"," is a blueprint; an ",[39,44,45],{},"instance"," is a concrete object built from it, with its own\nstate:",[48,49,54],"pre",{"className":50,"code":51,"language":52,"meta":53,"style":53},"language-python shiki shiki-themes github-light github-dark","class Dog:\n    def __init__(self, name):\n        self.name = name\n\nrex = Dog(\"Rex\")      # an instance\nfido = Dog(\"Fido\")    # a separate instance\nrex.name, fido.name   # 'Rex', 'Fido' — independent state\n","python","",[19,55,56,72,85,100,107,129,148],{"__ignoreMap":53},[57,58,61,64,68],"span",{"class":59,"line":60},"line",1,[57,62,41],{"class":63},"szBVR",[57,65,67],{"class":66},"sScJk"," Dog",[57,69,71],{"class":70},"sVt8B",":\n",[57,73,75,78,82],{"class":59,"line":74},2,[57,76,77],{"class":63},"    def",[57,79,81],{"class":80},"sj4cs"," __init__",[57,83,84],{"class":70},"(self, name):\n",[57,86,88,91,94,97],{"class":59,"line":87},3,[57,89,90],{"class":80},"        self",[57,92,93],{"class":70},".name ",[57,95,96],{"class":63},"=",[57,98,99],{"class":70}," name\n",[57,101,103],{"class":59,"line":102},4,[57,104,106],{"emptyLinePlaceholder":105},true,"\n",[57,108,110,113,115,118,122,125],{"class":59,"line":109},5,[57,111,112],{"class":70},"rex ",[57,114,96],{"class":63},[57,116,117],{"class":70}," Dog(",[57,119,121],{"class":120},"sZZnC","\"Rex\"",[57,123,124],{"class":70},")      ",[57,126,128],{"class":127},"sJ8bj","# an instance\n",[57,130,132,135,137,139,142,145],{"class":59,"line":131},6,[57,133,134],{"class":70},"fido ",[57,136,96],{"class":63},[57,138,117],{"class":70},[57,140,141],{"class":120},"\"Fido\"",[57,143,144],{"class":70},")    ",[57,146,147],{"class":127},"# a separate instance\n",[57,149,151,154],{"class":59,"line":150},7,[57,152,153],{"class":70},"rex.name, fido.name   ",[57,155,156],{"class":127},"# 'Rex', 'Fido' — independent state\n",[15,158,159],{},"You write the class once and create many instances, each carrying its own data but sharing\nthe class's methods.",[10,161,163],{"id":162},"what-self-is","What self is",[15,165,166,168,169,172],{},[19,167,21],{}," is the ",[39,170,171],{},"instance the method was called on",". It isn't a keyword — it's just the\nconventional name of the first parameter, which Python passes automatically:",[48,174,176],{"className":50,"code":175,"language":52,"meta":53,"style":53},"class Counter:\n    def __init__(self):\n        self.count = 0\n    def increment(self):\n        self.count += 1     # self is this particular instance\n\nc = Counter()\nc.increment()               # Python passes c as self\nCounter.increment(c)        # exactly equivalent — self made explicit\n",[19,177,178,187,196,208,217,232,236,246,255],{"__ignoreMap":53},[57,179,180,182,185],{"class":59,"line":60},[57,181,41],{"class":63},[57,183,184],{"class":66}," Counter",[57,186,71],{"class":70},[57,188,189,191,193],{"class":59,"line":74},[57,190,77],{"class":63},[57,192,81],{"class":80},[57,194,195],{"class":70},"(self):\n",[57,197,198,200,203,205],{"class":59,"line":87},[57,199,90],{"class":80},[57,201,202],{"class":70},".count ",[57,204,96],{"class":63},[57,206,207],{"class":80}," 0\n",[57,209,210,212,215],{"class":59,"line":102},[57,211,77],{"class":63},[57,213,214],{"class":66}," increment",[57,216,195],{"class":70},[57,218,219,221,223,226,229],{"class":59,"line":109},[57,220,90],{"class":80},[57,222,202],{"class":70},[57,224,225],{"class":63},"+=",[57,227,228],{"class":80}," 1",[57,230,231],{"class":127},"     # self is this particular instance\n",[57,233,234],{"class":59,"line":131},[57,235,106],{"emptyLinePlaceholder":105},[57,237,238,241,243],{"class":59,"line":150},[57,239,240],{"class":70},"c ",[57,242,96],{"class":63},[57,244,245],{"class":70}," Counter()\n",[57,247,249,252],{"class":59,"line":248},8,[57,250,251],{"class":70},"c.increment()               ",[57,253,254],{"class":127},"# Python passes c as self\n",[57,256,258,261],{"class":59,"line":257},9,[57,259,260],{"class":70},"Counter.increment(c)        ",[57,262,263],{"class":127},"# exactly equivalent — self made explicit\n",[15,265,266,267,270,271,30],{},"So ",[19,268,269],{},"c.increment()"," is sugar for ",[19,272,273],{},"Counter.increment(c)",[10,275,277,280,281],{"id":276},"init-vs-new",[39,278,279],{},"init"," vs ",[39,282,283],{},"new",[15,285,286,287,290,291,294,295,290,297,300],{},"These are often confused. ",[19,288,289],{},"__new__"," ",[39,292,293],{},"creates"," the object; ",[19,296,25],{},[39,298,299],{},"initialises"," the\nalready-created object:",[48,302,304],{"className":50,"code":303,"language":52,"meta":53,"style":53},"class Widget:\n    def __new__(cls, *args):\n        print(\"1. __new__ allocates\")\n        return super().__new__(cls)     # returns the new instance\n    def __init__(self, size):\n        print(\"2. __init__ configures\")\n        self.size = size\n\nWidget(10)   # prints 1 then 2\n",[19,305,306,315,331,345,369,378,389,401,405],{"__ignoreMap":53},[57,307,308,310,313],{"class":59,"line":60},[57,309,41],{"class":63},[57,311,312],{"class":66}," Widget",[57,314,71],{"class":70},[57,316,317,319,322,325,328],{"class":59,"line":74},[57,318,77],{"class":63},[57,320,321],{"class":80}," __new__",[57,323,324],{"class":70},"(cls, ",[57,326,327],{"class":63},"*",[57,329,330],{"class":70},"args):\n",[57,332,333,336,339,342],{"class":59,"line":87},[57,334,335],{"class":80},"        print",[57,337,338],{"class":70},"(",[57,340,341],{"class":120},"\"1. __new__ allocates\"",[57,343,344],{"class":70},")\n",[57,346,347,350,353,356,358,360,363,366],{"class":59,"line":102},[57,348,349],{"class":63},"        return",[57,351,352],{"class":80}," super",[57,354,355],{"class":70},"().",[57,357,289],{"class":80},[57,359,338],{"class":70},[57,361,362],{"class":80},"cls",[57,364,365],{"class":70},")     ",[57,367,368],{"class":127},"# returns the new instance\n",[57,370,371,373,375],{"class":59,"line":109},[57,372,77],{"class":63},[57,374,81],{"class":80},[57,376,377],{"class":70},"(self, size):\n",[57,379,380,382,384,387],{"class":59,"line":131},[57,381,335],{"class":80},[57,383,338],{"class":70},[57,385,386],{"class":120},"\"2. __init__ configures\"",[57,388,344],{"class":70},[57,390,391,393,396,398],{"class":59,"line":150},[57,392,90],{"class":80},[57,394,395],{"class":70},".size ",[57,397,96],{"class":63},[57,399,400],{"class":70}," size\n",[57,402,403],{"class":59,"line":248},[57,404,106],{"emptyLinePlaceholder":105},[57,406,407,410,413,416],{"class":59,"line":257},[57,408,409],{"class":70},"Widget(",[57,411,412],{"class":80},"10",[57,414,415],{"class":70},")   ",[57,417,418],{"class":127},"# prints 1 then 2\n",[15,420,421,423,424,426,427,429,430,432,433,436,437,439],{},[19,422,289],{}," runs first, is a static method receiving the ",[39,425,41],{},", and returns the instance.\n",[19,428,25],{}," runs second, receives that instance as ",[19,431,21],{},", and must return ",[19,434,435],{},"None",". You\nrarely override ",[19,438,289],{}," — it's mainly for immutable types and singletons.",[10,441,443],{"id":442},"instance-vs-class-attributes","Instance vs class attributes",[15,445,37,446,449,450,453,454,456],{},[39,447,448],{},"class attribute"," lives on the class and is shared by all instances; an ",[39,451,452],{},"instance\nattribute"," lives on ",[19,455,21],{}," and is per-object. Lookup checks the instance first, then the\nclass:",[48,458,460],{"className":50,"code":459,"language":52,"meta":53,"style":53},"class Dog:\n    species = \"Canis familiaris\"   # class attribute — shared\n    def __init__(self, name):\n        self.name = name           # instance attribute — per object\n\na, b = Dog(\"Rex\"), Dog(\"Fido\")\na.species                # 'Canis familiaris' (from the class)\na.species = \"wolf\"       # creates an INSTANCE attr that shadows the class one\nb.species                # still 'Canis familiaris'\n",[19,461,462,470,483,491,505,509,527,535,548],{"__ignoreMap":53},[57,463,464,466,468],{"class":59,"line":60},[57,465,41],{"class":63},[57,467,67],{"class":66},[57,469,71],{"class":70},[57,471,472,475,477,480],{"class":59,"line":74},[57,473,474],{"class":70},"    species ",[57,476,96],{"class":63},[57,478,479],{"class":120}," \"Canis familiaris\"",[57,481,482],{"class":127},"   # class attribute — shared\n",[57,484,485,487,489],{"class":59,"line":87},[57,486,77],{"class":63},[57,488,81],{"class":80},[57,490,84],{"class":70},[57,492,493,495,497,499,502],{"class":59,"line":102},[57,494,90],{"class":80},[57,496,93],{"class":70},[57,498,96],{"class":63},[57,500,501],{"class":70}," name           ",[57,503,504],{"class":127},"# instance attribute — per object\n",[57,506,507],{"class":59,"line":109},[57,508,106],{"emptyLinePlaceholder":105},[57,510,511,514,516,518,520,523,525],{"class":59,"line":131},[57,512,513],{"class":70},"a, b ",[57,515,96],{"class":63},[57,517,117],{"class":70},[57,519,121],{"class":120},[57,521,522],{"class":70},"), Dog(",[57,524,141],{"class":120},[57,526,344],{"class":70},[57,528,529,532],{"class":59,"line":150},[57,530,531],{"class":70},"a.species                ",[57,533,534],{"class":127},"# 'Canis familiaris' (from the class)\n",[57,536,537,540,542,545],{"class":59,"line":248},[57,538,539],{"class":70},"a.species ",[57,541,96],{"class":63},[57,543,544],{"class":120}," \"wolf\"",[57,546,547],{"class":127},"       # creates an INSTANCE attr that shadows the class one\n",[57,549,550,553],{"class":59,"line":257},[57,551,552],{"class":70},"b.species                ",[57,554,555],{"class":127},"# still 'Canis familiaris'\n",[15,557,558,559,562,563,566,567,30],{},"The trap: a ",[39,560,561],{},"mutable"," class attribute (like ",[19,564,565],{},"[]",") is shared and leaks state between\ninstances — initialise mutable state in ",[19,568,25],{},[10,570,572,280,575],{"id":571},"repr-vs-str",[39,573,574],{},"repr",[39,576,577],{},"str",[15,579,580,583,584,587,588,591,592,594,595,597],{},[19,581,582],{},"__repr__"," is the unambiguous, developer-facing representation (shown in the REPL and\ncontainers); ",[19,585,586],{},"__str__"," is the readable, user-facing one used by ",[19,589,590],{},"print()",". If ",[19,593,586],{}," is\nmissing, Python falls back to ",[19,596,582],{},":",[48,599,601],{"className":50,"code":600,"language":52,"meta":53,"style":53},"class Point:\n    def __init__(self, x, y):\n        self.x, self.y = x, y\n    def __repr__(self):\n        return f\"Point(x={self.x}, y={self.y})\"\n    def __str__(self):\n        return f\"({self.x}, {self.y})\"\n\np = Point(1, 2)\nprint(p)     # (1, 2)            — __str__\nrepr(p)      # 'Point(x=1, y=2)' — __repr__\n[p]          # [Point(x=1, y=2)] — containers use __repr__\n",[19,602,603,612,621,638,647,679,688,713,717,737,749,760],{"__ignoreMap":53},[57,604,605,607,610],{"class":59,"line":60},[57,606,41],{"class":63},[57,608,609],{"class":66}," Point",[57,611,71],{"class":70},[57,613,614,616,618],{"class":59,"line":74},[57,615,77],{"class":63},[57,617,81],{"class":80},[57,619,620],{"class":70},"(self, x, y):\n",[57,622,623,625,628,630,633,635],{"class":59,"line":87},[57,624,90],{"class":80},[57,626,627],{"class":70},".x, ",[57,629,21],{"class":80},[57,631,632],{"class":70},".y ",[57,634,96],{"class":63},[57,636,637],{"class":70}," x, y\n",[57,639,640,642,645],{"class":59,"line":102},[57,641,77],{"class":63},[57,643,644],{"class":80}," __repr__",[57,646,195],{"class":70},[57,648,649,651,654,657,660,663,666,669,671,674,676],{"class":59,"line":109},[57,650,349],{"class":63},[57,652,653],{"class":63}," f",[57,655,656],{"class":120},"\"Point(x=",[57,658,659],{"class":80},"{self",[57,661,662],{"class":70},".x",[57,664,665],{"class":80},"}",[57,667,668],{"class":120},", y=",[57,670,659],{"class":80},[57,672,673],{"class":70},".y",[57,675,665],{"class":80},[57,677,678],{"class":120},")\"\n",[57,680,681,683,686],{"class":59,"line":131},[57,682,77],{"class":63},[57,684,685],{"class":80}," __str__",[57,687,195],{"class":70},[57,689,690,692,694,697,699,701,703,705,707,709,711],{"class":59,"line":150},[57,691,349],{"class":63},[57,693,653],{"class":63},[57,695,696],{"class":120},"\"(",[57,698,659],{"class":80},[57,700,662],{"class":70},[57,702,665],{"class":80},[57,704,22],{"class":120},[57,706,659],{"class":80},[57,708,673],{"class":70},[57,710,665],{"class":80},[57,712,678],{"class":120},[57,714,715],{"class":59,"line":248},[57,716,106],{"emptyLinePlaceholder":105},[57,718,719,722,724,727,730,732,735],{"class":59,"line":257},[57,720,721],{"class":70},"p ",[57,723,96],{"class":63},[57,725,726],{"class":70}," Point(",[57,728,729],{"class":80},"1",[57,731,22],{"class":70},[57,733,734],{"class":80},"2",[57,736,344],{"class":70},[57,738,740,743,746],{"class":59,"line":739},10,[57,741,742],{"class":80},"print",[57,744,745],{"class":70},"(p)     ",[57,747,748],{"class":127},"# (1, 2)            — __str__\n",[57,750,752,754,757],{"class":59,"line":751},11,[57,753,574],{"class":80},[57,755,756],{"class":70},"(p)      ",[57,758,759],{"class":127},"# 'Point(x=1, y=2)' — __repr__\n",[57,761,763,766],{"class":59,"line":762},12,[57,764,765],{"class":70},"[p]          ",[57,767,768],{"class":127},"# [Point(x=1, y=2)] — containers use __repr__\n",[15,770,771,772,774,775,777],{},"Rule of thumb: always define ",[19,773,582],{},"; add ",[19,776,586],{}," only when you want a distinct friendly\nform.",[10,779,781],{"id":780},"what-happens-when-you-call-classname","What happens when you call ClassName()",[15,783,784,787,788,791,792,794,795,797,798,801],{},[19,785,786],{},"ClassName(args)"," triggers two steps via the metaclass: call ",[19,789,790],{},"__new__(cls, args)"," to\nallocate the object, then — if ",[19,793,289],{}," returned an instance of ",[19,796,362],{}," — call\n",[19,799,800],{},"__init__(instance, args)"," to initialise it, and return the instance:",[48,803,805],{"className":50,"code":804,"language":52,"meta":53,"style":53},"class Demo:\n    def __new__(cls, *a):\n        print(\"__new__\")\n        return super().__new__(cls)\n    def __init__(self, *a):\n        print(\"__init__\")\n\nd = Demo()    # __new__ then __init__\n",[19,806,807,816,829,840,856,869,880,884],{"__ignoreMap":53},[57,808,809,811,814],{"class":59,"line":60},[57,810,41],{"class":63},[57,812,813],{"class":66}," Demo",[57,815,71],{"class":70},[57,817,818,820,822,824,826],{"class":59,"line":74},[57,819,77],{"class":63},[57,821,321],{"class":80},[57,823,324],{"class":70},[57,825,327],{"class":63},[57,827,828],{"class":70},"a):\n",[57,830,831,833,835,838],{"class":59,"line":87},[57,832,335],{"class":80},[57,834,338],{"class":70},[57,836,837],{"class":120},"\"__new__\"",[57,839,344],{"class":70},[57,841,842,844,846,848,850,852,854],{"class":59,"line":102},[57,843,349],{"class":63},[57,845,352],{"class":80},[57,847,355],{"class":70},[57,849,289],{"class":80},[57,851,338],{"class":70},[57,853,362],{"class":80},[57,855,344],{"class":70},[57,857,858,860,862,865,867],{"class":59,"line":109},[57,859,77],{"class":63},[57,861,81],{"class":80},[57,863,864],{"class":70},"(self, ",[57,866,327],{"class":63},[57,868,828],{"class":70},[57,870,871,873,875,878],{"class":59,"line":131},[57,872,335],{"class":80},[57,874,338],{"class":70},[57,876,877],{"class":120},"\"__init__\"",[57,879,344],{"class":70},[57,881,882],{"class":59,"line":150},[57,883,106],{"emptyLinePlaceholder":105},[57,885,886,889,891,894],{"class":59,"line":248},[57,887,888],{"class":70},"d ",[57,890,96],{"class":63},[57,892,893],{"class":70}," Demo()    ",[57,895,896],{"class":127},"# __new__ then __init__\n",[15,898,899,900,902,903,906,907,909],{},"If ",[19,901,289],{}," returns something that ",[39,904,905],{},"isn't"," an instance of the class, ",[19,908,25],{}," is\nskipped entirely.",[10,911,913],{"id":912},"recap","Recap",[15,915,916,917,921,922,926,927,931,932,935,936,939,940,944,945,949,950,952,953,955,956,30],{},"A class is a blueprint; instances are objects built from it with independent state.\n",[39,918,919],{},[19,920,21],{}," is just the first parameter Python auto-binds to the instance. ",[39,923,924],{},[19,925,289],{},"\ncreates the object and ",[39,928,929],{},[19,930,25],{}," initialises it. ",[39,933,934],{},"Class attributes"," are shared (watch\nmutable ones); ",[39,937,938],{},"instance attributes"," are per-object and shadow class attributes on\nassignment. Always define ",[39,941,942],{},[19,943,582],{}," (developer-facing) and add ",[39,946,947],{},[19,948,586],{}," for a\nfriendly form. Calling ",[19,951,29],{}," runs ",[19,954,289],{}," then ",[19,957,25],{},[959,960,961],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}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);}",{"title":53,"searchDepth":74,"depth":74,"links":963},[964,965,966,967,969,970,972,973],{"id":12,"depth":74,"text":13},{"id":33,"depth":74,"text":34},{"id":162,"depth":74,"text":163},{"id":276,"depth":74,"text":968},"init vs new",{"id":442,"depth":74,"text":443},{"id":571,"depth":74,"text":971},"repr vs str",{"id":780,"depth":74,"text":781},{"id":912,"depth":74,"text":913},"How Python classes and instances work — what self is, __init__ vs __new__, instance vs class attributes, __repr__ vs __str__, and the object creation flow.","easy","md","Python",{},"\u002Fblog\u002Fpython-classes-instances-init-explained","\u002Fpython\u002Foop\u002Fclasses",{"title":5,"description":974},"blog\u002Fpython-classes-instances-init-explained","Classes, Instances & __init__","Object-Oriented Programming","oop","2026-06-19","NVlaTN6ZTB4TuEC9cxd9omIGS1-q5WbiajTPEEhFdNI",1782244092367]