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