[{"data":1,"prerenderedAt":1343},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-decorator-example-simple-use-case":3},{"id":4,"title":5,"body":6,"description":1336,"extension":1337,"meta":1338,"navigation":169,"path":1339,"seo":1340,"stem":1341,"__hash__":1342},"content\u002Fexamples\u002Fpython-decorator-example-simple-use-case.md","Python Decorator Example (Simple Use Case)",{"type":7,"value":8,"toc":1296},"minimark",[9,13,17,25,30,33,46,55,59,221,224,253,257,260,278,286,290,295,312,315,321,329,342,345,349,369,372,376,387,390,393,406,410,429,432,438,450,453,456,459,466,499,502,550,553,557,568,574,586,590,593,601,604,616,620,623,626,643,646,657,661,664,670,673,705,708,759,765,778,782,785,801,804,831,834,877,880,895,899,902,908,911,970,973,983,989,998,1000,1059,1062,1066,1069,1101,1104,1114,1118,1121,1184,1190,1198,1201,1213,1216,1220,1224,1227,1231,1234,1245,1251,1255,1262,1266,1292],[10,11,5],"h1",{"id":12},"python-decorator-example-simple-use-case",[14,15,16],"p",{},"If you are new to decorators, the easiest way to understand them is to see one small example that works.",[14,18,19,20,24],{},"This page shows a simple decorator that wraps a function, runs code before and after it, and helps you see why decorators are useful. It does ",[21,22,23],"strong",{},"not"," try to cover every decorator pattern.",[26,27,29],"h2",{"id":28},"page-goal","Page goal",[14,31,32],{},"This example focuses on one clear idea:",[34,35,36,40,43],"ul",{},[37,38,39],"li",{},"Understand a simple decorator by example",[37,41,42],{},"Read the code step by step",[37,44,45],{},"Run it yourself and see the output",[14,47,48,49,54],{},"If you want the full beginner explanation later, see ",[50,51,53],"a",{"href":52},"\u002Flearn\u002Fdecorators-in-python-beginner-introduction\u002F","Decorators in Python: beginner introduction",".",[26,56,58],{"id":57},"a-simple-runnable-decorator-example","A simple runnable decorator example",[60,61,66],"pre",{"className":62,"code":63,"language":64,"meta":65,"style":65},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        func()\n        print(\"After function runs\")\n    return wrapper\n\n@show_before_after\ndef say_hello():\n    print(\"Hello!\")\n\nsay_hello()\n","python","",[67,68,69,93,105,127,137,153,164,171,181,191,208,213],"code",{"__ignoreMap":65},[70,71,74,78,82,86,90],"span",{"class":72,"line":73},"line",1,[70,75,77],{"class":76},"sbsja","def",[70,79,81],{"class":80},"sGLFI"," show_before_after",[70,83,85],{"class":84},"sP7_E","(",[70,87,89],{"class":88},"sFwrP","func",[70,91,92],{"class":84},"):\n",[70,94,96,99,102],{"class":72,"line":95},2,[70,97,98],{"class":76},"    def",[70,100,101],{"class":80}," wrapper",[70,103,104],{"class":84},"():\n",[70,106,108,112,114,118,122,124],{"class":72,"line":107},3,[70,109,111],{"class":110},"sptTA","        print",[70,113,85],{"class":84},[70,115,117],{"class":116},"sjJ54","\"",[70,119,121],{"class":120},"s_sjI","Before function runs",[70,123,117],{"class":116},[70,125,126],{"class":84},")\n",[70,128,130,134],{"class":72,"line":129},4,[70,131,133],{"class":132},"slqww","        func",[70,135,136],{"class":84},"()\n",[70,138,140,142,144,146,149,151],{"class":72,"line":139},5,[70,141,111],{"class":110},[70,143,85],{"class":84},[70,145,117],{"class":116},[70,147,148],{"class":120},"After function runs",[70,150,117],{"class":116},[70,152,126],{"class":84},[70,154,156,160],{"class":72,"line":155},6,[70,157,159],{"class":158},"sVHd0","    return",[70,161,163],{"class":162},"su5hD"," wrapper\n",[70,165,167],{"class":72,"line":166},7,[70,168,170],{"emptyLinePlaceholder":169},true,"\n",[70,172,174,178],{"class":72,"line":173},8,[70,175,177],{"class":176},"stp6e","@",[70,179,180],{"class":80},"show_before_after\n",[70,182,184,186,189],{"class":72,"line":183},9,[70,185,77],{"class":76},[70,187,188],{"class":80}," say_hello",[70,190,104],{"class":84},[70,192,194,197,199,201,204,206],{"class":72,"line":193},10,[70,195,196],{"class":110},"    print",[70,198,85],{"class":84},[70,200,117],{"class":116},[70,202,203],{"class":120},"Hello!",[70,205,117],{"class":116},[70,207,126],{"class":84},[70,209,211],{"class":72,"line":210},11,[70,212,170],{"emptyLinePlaceholder":169},[70,214,216,219],{"class":72,"line":215},12,[70,217,218],{"class":132},"say_hello",[70,220,136],{"class":84},[14,222,223],{},"This is the main pattern to notice:",[34,225,226,232,238,243,248],{},[37,227,228,231],{},[67,229,230],{},"show_before_after"," takes a function as input",[37,233,234,235],{},"It defines a new function called ",[67,236,237],{},"wrapper",[37,239,240,242],{},[67,241,237],{}," runs extra code",[37,244,245,247],{},[67,246,237],{}," also calls the original function",[37,249,250,251],{},"The decorator returns ",[67,252,237],{},[26,254,256],{"id":255},"what-the-example-shows","What the example shows",[14,258,259],{},"This one example teaches the basic decorator idea:",[34,261,262,265,268,271],{},[37,263,264],{},"A decorator takes a function",[37,266,267],{},"It returns a new function",[37,269,270],{},"The new function runs extra code before and after the original function",[37,272,273,274,277],{},"The ",[67,275,276],{},"@decorator_name"," syntax is a shortcut",[14,279,280,281,285],{},"If you already understand ",[50,282,284],{"href":283},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F","what a function is in Python",", this pattern will make more sense.",[26,287,289],{"id":288},"how-to-read-the-code-step-by-step","How to read the code step by step",[291,292,294],"h3",{"id":293},"_1-define-the-decorator","1. Define the decorator",[60,296,298],{"className":62,"code":297,"language":64,"meta":65,"style":65},"def show_before_after(func):\n",[67,299,300],{"__ignoreMap":65},[70,301,302,304,306,308,310],{"class":72,"line":73},[70,303,77],{"class":76},[70,305,81],{"class":80},[70,307,85],{"class":84},[70,309,89],{"class":88},[70,311,92],{"class":84},[14,313,314],{},"This creates a decorator function.",[14,316,317,318,320],{},"The parameter ",[67,319,89],{}," is the function being decorated.",[291,322,324,325,328],{"id":323},"_2-define-wrapper-inside-the-decorator","2. Define ",[67,326,327],{},"wrapper()"," inside the decorator",[60,330,332],{"className":62,"code":331,"language":64,"meta":65,"style":65},"def wrapper():\n",[67,333,334],{"__ignoreMap":65},[70,335,336,338,340],{"class":72,"line":73},[70,337,77],{"class":76},[70,339,101],{"class":80},[70,341,104],{"class":84},[14,343,344],{},"This inner function is the new function that will replace the original one.",[291,346,348],{"id":347},"_3-run-code-before-the-original-function","3. Run code before the original function",[60,350,352],{"className":62,"code":351,"language":64,"meta":65,"style":65},"print(\"Before function runs\")\n",[67,353,354],{"__ignoreMap":65},[70,355,356,359,361,363,365,367],{"class":72,"line":73},[70,357,358],{"class":110},"print",[70,360,85],{"class":84},[70,362,117],{"class":116},[70,364,121],{"class":120},[70,366,117],{"class":116},[70,368,126],{"class":84},[14,370,371],{},"This is extra behavior added by the decorator.",[291,373,375],{"id":374},"_4-call-the-original-function","4. Call the original function",[60,377,379],{"className":62,"code":378,"language":64,"meta":65,"style":65},"func()\n",[67,380,381],{"__ignoreMap":65},[70,382,383,385],{"class":72,"line":73},[70,384,89],{"class":132},[70,386,136],{"class":84},[14,388,389],{},"This runs the original function that was passed into the decorator.",[14,391,392],{},"Be careful here:",[34,394,395,400],{},[37,396,397,399],{},[67,398,89],{}," means the function itself",[37,401,402,405],{},[67,403,404],{},"func()"," means “run the function”",[291,407,409],{"id":408},"_5-run-code-after-the-original-function","5. Run code after the original function",[60,411,413],{"className":62,"code":412,"language":64,"meta":65,"style":65},"print(\"After function runs\")\n",[67,414,415],{"__ignoreMap":65},[70,416,417,419,421,423,425,427],{"class":72,"line":73},[70,418,358],{"class":110},[70,420,85],{"class":84},[70,422,117],{"class":116},[70,424,148],{"class":120},[70,426,117],{"class":116},[70,428,126],{"class":84},[14,430,431],{},"This is another piece of added behavior.",[291,433,435,436],{"id":434},"_6-return-wrapper","6. Return ",[67,437,237],{},[60,439,441],{"className":62,"code":440,"language":64,"meta":65,"style":65},"return wrapper\n",[67,442,443],{"__ignoreMap":65},[70,444,445,448],{"class":72,"line":73},[70,446,447],{"class":158},"return",[70,449,163],{"class":162},[14,451,452],{},"This is a very important line.",[14,454,455],{},"It means the decorator gives back the new wrapped function.",[14,457,458],{},"If you forget this line, the decorator will not work correctly.",[291,460,462,463],{"id":461},"_7-apply-the-decorator-with-show_before_after","7. Apply the decorator with ",[67,464,465],{},"@show_before_after",[60,467,469],{"className":62,"code":468,"language":64,"meta":65,"style":65},"@show_before_after\ndef say_hello():\n    print(\"Hello!\")\n",[67,470,471,477,485],{"__ignoreMap":65},[70,472,473,475],{"class":72,"line":73},[70,474,177],{"class":176},[70,476,180],{"class":80},[70,478,479,481,483],{"class":72,"line":95},[70,480,77],{"class":76},[70,482,188],{"class":80},[70,484,104],{"class":84},[70,486,487,489,491,493,495,497],{"class":72,"line":107},[70,488,196],{"class":110},[70,490,85],{"class":84},[70,492,117],{"class":116},[70,494,203],{"class":120},[70,496,117],{"class":116},[70,498,126],{"class":84},[14,500,501],{},"This is a shorter way to write:",[60,503,505],{"className":62,"code":504,"language":64,"meta":65,"style":65},"def say_hello():\n    print(\"Hello!\")\n\nsay_hello = show_before_after(say_hello)\n",[67,506,507,515,529,533],{"__ignoreMap":65},[70,508,509,511,513],{"class":72,"line":73},[70,510,77],{"class":76},[70,512,188],{"class":80},[70,514,104],{"class":84},[70,516,517,519,521,523,525,527],{"class":72,"line":95},[70,518,196],{"class":110},[70,520,85],{"class":84},[70,522,117],{"class":116},[70,524,203],{"class":120},[70,526,117],{"class":116},[70,528,126],{"class":84},[70,530,531],{"class":72,"line":107},[70,532,170],{"emptyLinePlaceholder":169},[70,534,535,538,542,544,546,548],{"class":72,"line":129},[70,536,537],{"class":162},"say_hello ",[70,539,541],{"class":540},"smGrS","=",[70,543,81],{"class":132},[70,545,85],{"class":84},[70,547,218],{"class":132},[70,549,126],{"class":84},[14,551,552],{},"Both versions do the same thing.",[291,554,556],{"id":555},"_8-call-the-decorated-function-normally","8. Call the decorated function normally",[60,558,560],{"className":62,"code":559,"language":64,"meta":65,"style":65},"say_hello()\n",[67,561,562],{"__ignoreMap":65},[70,563,564,566],{"class":72,"line":73},[70,565,218],{"class":132},[70,567,136],{"class":84},[14,569,570,571,573],{},"Even though ",[67,572,218],{}," was decorated, you still call it like a normal function.",[14,575,576,577,581,582,54],{},"If you need a refresher, see ",[50,578,580],{"href":579},"\u002Flearn\u002Fpython-functions-explained\u002F","Python functions explained"," or ",[50,583,585],{"href":584},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python",[26,587,589],{"id":588},"expected-output","Expected output",[14,591,592],{},"When you run the code, you should see:",[60,594,599],{"className":595,"code":597,"language":598,"meta":65},[596],"language-text","Before function runs\nHello!\nAfter function runs\n","text",[67,600,597],{"__ignoreMap":65},[14,602,603],{},"That output shows the order clearly:",[605,606,607,610,613],"ol",{},[37,608,609],{},"The decorator runs code before",[37,611,612],{},"The original function runs",[37,614,615],{},"The decorator runs code after",[26,617,619],{"id":618},"why-beginners-use-decorators","Why beginners use decorators",[14,621,622],{},"Beginners usually meet decorators when they want to add repeated behavior without rewriting the same code again and again.",[14,624,625],{},"A decorator can help you:",[34,627,628,631,634,637,640],{},[37,629,630],{},"Add messages before or after a function runs",[37,632,633],{},"Log information",[37,635,636],{},"Time how long a function takes",[37,638,639],{},"Check permissions or access",[37,641,642],{},"Reuse the same pattern across many functions",[14,644,645],{},"The big benefit is this:",[34,647,648,654],{},[37,649,650,651,653],{},"You do ",[21,652,23],{}," need to change the original function body each time",[37,655,656],{},"You avoid copying the same extra code into many functions",[26,658,660],{"id":659},"important-limitation-of-this-simple-example","Important limitation of this simple example",[14,662,663],{},"This example is intentionally basic.",[14,665,666,667,54],{},"It only works with functions that take ",[21,668,669],{},"no arguments",[14,671,672],{},"For example, this version works:",[60,674,675],{"className":62,"code":468,"language":64,"meta":65,"style":65},[67,676,677,683,691],{"__ignoreMap":65},[70,678,679,681],{"class":72,"line":73},[70,680,177],{"class":176},[70,682,180],{"class":80},[70,684,685,687,689],{"class":72,"line":95},[70,686,77],{"class":76},[70,688,188],{"class":80},[70,690,104],{"class":84},[70,692,693,695,697,699,701,703],{"class":72,"line":107},[70,694,196],{"class":110},[70,696,85],{"class":84},[70,698,117],{"class":116},[70,700,203],{"class":120},[70,702,117],{"class":116},[70,704,126],{"class":84},[14,706,707],{},"But this version would fail:",[60,709,711],{"className":62,"code":710,"language":64,"meta":65,"style":65},"@show_before_after\ndef greet(name):\n    print(f\"Hello, {name}!\")\n",[67,712,713,719,733],{"__ignoreMap":65},[70,714,715,717],{"class":72,"line":73},[70,716,177],{"class":176},[70,718,180],{"class":80},[70,720,721,723,726,728,731],{"class":72,"line":95},[70,722,77],{"class":76},[70,724,725],{"class":80}," greet",[70,727,85],{"class":84},[70,729,730],{"class":88},"name",[70,732,92],{"class":84},[70,734,735,737,739,742,745,749,751,754,757],{"class":72,"line":107},[70,736,196],{"class":110},[70,738,85],{"class":84},[70,740,741],{"class":76},"f",[70,743,744],{"class":120},"\"Hello, ",[70,746,748],{"class":747},"srdBf","{",[70,750,730],{"class":132},[70,752,753],{"class":747},"}",[70,755,756],{"class":120},"!\"",[70,758,126],{"class":84},[14,760,761,762,764],{},"Why? Because ",[67,763,327],{}," does not accept any parameters.",[14,766,767,768,771,772,775,776,54],{},"A more flexible decorator uses ",[67,769,770],{},"*args"," and ",[67,773,774],{},"**kwargs",", but that is better explained on a dedicated lesson page. For the full beginner explanation, see ",[50,777,53],{"href":52},[26,779,781],{"id":780},"common-beginner-confusion","Common beginner confusion",[14,783,784],{},"These points often cause confusion:",[34,786,787,790,795],{},[37,788,789],{},"The decorator runs when the function is defined, not only when it is later called",[37,791,792,794],{},[67,793,237],{}," is the function that replaces the original name",[37,796,797,798,800],{},"The original function still exists as ",[67,799,89],{}," inside the wrapper",[14,802,803],{},"In simple terms:",[34,805,806,812,820],{},[37,807,808,809,811],{},"Before decoration, ",[67,810,218],{}," refers to the original function",[37,813,814,815,817,818],{},"After decoration, ",[67,816,218],{}," refers to ",[67,819,237],{},[37,821,822,823,825,826,828,829],{},"Inside ",[67,824,237],{},", ",[67,827,89],{}," still points to the original ",[67,830,218],{},[14,832,833],{},"You can inspect that a little with:",[60,835,837],{"className":62,"code":836,"language":64,"meta":65,"style":65},"print(say_hello)\nprint(type(say_hello))\nhelp(say_hello)\n",[67,838,839,849,866],{"__ignoreMap":65},[70,840,841,843,845,847],{"class":72,"line":73},[70,842,358],{"class":110},[70,844,85],{"class":84},[70,846,218],{"class":132},[70,848,126],{"class":84},[70,850,851,853,855,859,861,863],{"class":72,"line":95},[70,852,358],{"class":110},[70,854,85],{"class":84},[70,856,858],{"class":857},"sZMiF","type",[70,860,85],{"class":84},[70,862,218],{"class":132},[70,864,865],{"class":84},"))\n",[70,867,868,871,873,875],{"class":72,"line":107},[70,869,870],{"class":110},"help",[70,872,85],{"class":84},[70,874,218],{"class":132},[70,876,126],{"class":84},[14,878,879],{},"And to run your file:",[60,881,885],{"className":882,"code":883,"language":884,"meta":65,"style":65},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[67,886,887],{"__ignoreMap":65},[70,888,889,892],{"class":72,"line":73},[70,890,64],{"class":891},"sbgvK",[70,893,894],{"class":120}," your_script.py\n",[26,896,898],{"id":897},"common-mistakes","Common mistakes",[14,900,901],{},"Here are some common problems beginners run into.",[291,903,905,906],{"id":904},"forgetting-to-return-wrapper","Forgetting to return ",[67,907,237],{},[14,909,910],{},"Wrong:",[60,912,914],{"className":62,"code":913,"language":64,"meta":65,"style":65},"def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        func()\n        print(\"After function runs\")\n",[67,915,916,928,936,950,956],{"__ignoreMap":65},[70,917,918,920,922,924,926],{"class":72,"line":73},[70,919,77],{"class":76},[70,921,81],{"class":80},[70,923,85],{"class":84},[70,925,89],{"class":88},[70,927,92],{"class":84},[70,929,930,932,934],{"class":72,"line":95},[70,931,98],{"class":76},[70,933,101],{"class":80},[70,935,104],{"class":84},[70,937,938,940,942,944,946,948],{"class":72,"line":107},[70,939,111],{"class":110},[70,941,85],{"class":84},[70,943,117],{"class":116},[70,945,121],{"class":120},[70,947,117],{"class":116},[70,949,126],{"class":84},[70,951,952,954],{"class":72,"line":129},[70,953,133],{"class":132},[70,955,136],{"class":84},[70,957,958,960,962,964,966,968],{"class":72,"line":139},[70,959,111],{"class":110},[70,961,85],{"class":84},[70,963,117],{"class":116},[70,965,148],{"class":120},[70,967,117],{"class":116},[70,969,126],{"class":84},[14,971,972],{},"This is missing:",[60,974,975],{"className":62,"code":440,"language":64,"meta":65,"style":65},[67,976,977],{"__ignoreMap":65},[70,978,979,981],{"class":72,"line":73},[70,980,447],{"class":158},[70,982,163],{"class":162},[14,984,985,986,988],{},"Without returning ",[67,987,237],{},", the decorated function will not be set up correctly.",[291,990,992,993,995,996],{"id":991},"forgetting-to-call-func-inside-wrapper","Forgetting to call ",[67,994,404],{}," inside ",[67,997,237],{},[14,999,910],{},[60,1001,1003],{"className":62,"code":1002,"language":64,"meta":65,"style":65},"def show_before_after(func):\n    def wrapper():\n        print(\"Before function runs\")\n        print(\"After function runs\")\n    return wrapper\n",[67,1004,1005,1017,1025,1039,1053],{"__ignoreMap":65},[70,1006,1007,1009,1011,1013,1015],{"class":72,"line":73},[70,1008,77],{"class":76},[70,1010,81],{"class":80},[70,1012,85],{"class":84},[70,1014,89],{"class":88},[70,1016,92],{"class":84},[70,1018,1019,1021,1023],{"class":72,"line":95},[70,1020,98],{"class":76},[70,1022,101],{"class":80},[70,1024,104],{"class":84},[70,1026,1027,1029,1031,1033,1035,1037],{"class":72,"line":107},[70,1028,111],{"class":110},[70,1030,85],{"class":84},[70,1032,117],{"class":116},[70,1034,121],{"class":120},[70,1036,117],{"class":116},[70,1038,126],{"class":84},[70,1040,1041,1043,1045,1047,1049,1051],{"class":72,"line":129},[70,1042,111],{"class":110},[70,1044,85],{"class":84},[70,1046,117],{"class":116},[70,1048,148],{"class":120},[70,1050,117],{"class":116},[70,1052,126],{"class":84},[70,1054,1055,1057],{"class":72,"line":139},[70,1056,159],{"class":158},[70,1058,163],{"class":162},[14,1060,1061],{},"This prints the messages, but it never runs the original function.",[291,1063,1065],{"id":1064},"adding-the-decorator-but-never-calling-the-function","Adding the decorator but never calling the function",[14,1067,1068],{},"This code defines the function:",[60,1070,1071],{"className":62,"code":468,"language":64,"meta":65,"style":65},[67,1072,1073,1079,1087],{"__ignoreMap":65},[70,1074,1075,1077],{"class":72,"line":73},[70,1076,177],{"class":176},[70,1078,180],{"class":80},[70,1080,1081,1083,1085],{"class":72,"line":95},[70,1082,77],{"class":76},[70,1084,188],{"class":80},[70,1086,104],{"class":84},[70,1088,1089,1091,1093,1095,1097,1099],{"class":72,"line":107},[70,1090,196],{"class":110},[70,1092,85],{"class":84},[70,1094,117],{"class":116},[70,1096,203],{"class":120},[70,1098,117],{"class":116},[70,1100,126],{"class":84},[14,1102,1103],{},"But nothing happens until you call:",[60,1105,1106],{"className":62,"code":559,"language":64,"meta":65,"style":65},[67,1107,1108],{"__ignoreMap":65},[70,1109,1110,1112],{"class":72,"line":73},[70,1111,218],{"class":132},[70,1113,136],{"class":84},[291,1115,1117],{"id":1116},"using-a-wrapper-with-no-parameters-on-a-function-that-needs-arguments","Using a wrapper with no parameters on a function that needs arguments",[14,1119,1120],{},"This will cause a problem:",[60,1122,1124],{"className":62,"code":1123,"language":64,"meta":65,"style":65},"@show_before_after\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"Sam\")\n",[67,1125,1126,1132,1144,1164,1168],{"__ignoreMap":65},[70,1127,1128,1130],{"class":72,"line":73},[70,1129,177],{"class":176},[70,1131,180],{"class":80},[70,1133,1134,1136,1138,1140,1142],{"class":72,"line":95},[70,1135,77],{"class":76},[70,1137,725],{"class":80},[70,1139,85],{"class":84},[70,1141,730],{"class":88},[70,1143,92],{"class":84},[70,1145,1146,1148,1150,1152,1154,1156,1158,1160,1162],{"class":72,"line":107},[70,1147,196],{"class":110},[70,1149,85],{"class":84},[70,1151,741],{"class":76},[70,1153,744],{"class":120},[70,1155,748],{"class":747},[70,1157,730],{"class":132},[70,1159,753],{"class":747},[70,1161,756],{"class":120},[70,1163,126],{"class":84},[70,1165,1166],{"class":72,"line":129},[70,1167,170],{"emptyLinePlaceholder":169},[70,1169,1170,1173,1175,1177,1180,1182],{"class":72,"line":139},[70,1171,1172],{"class":132},"greet",[70,1174,85],{"class":84},[70,1176,117],{"class":116},[70,1178,1179],{"class":120},"Sam",[70,1181,117],{"class":116},[70,1183,126],{"class":84},[14,1185,1186,1187,54],{},"Your wrapper has no parameters, so it cannot accept ",[67,1188,1189],{},"\"Sam\"",[291,1191,1193,1194,771,1196],{"id":1192},"mixing-up-func-and-func","Mixing up ",[67,1195,89],{},[67,1197,404],{},[14,1199,1200],{},"Remember:",[34,1202,1203,1208],{},[37,1204,1205,1207],{},[67,1206,89],{}," = the function object",[37,1209,1210,1212],{},[67,1211,404],{}," = run the function",[14,1214,1215],{},"That small difference matters a lot in Python.",[26,1217,1219],{"id":1218},"faq","FAQ",[291,1221,1223],{"id":1222},"what-is-the-simplest-way-to-describe-a-decorator","What is the simplest way to describe a decorator?",[14,1225,1226],{},"A decorator is a function that changes or extends another function.",[291,1228,1230],{"id":1229},"do-i-need-decorators-as-a-beginner","Do I need decorators as a beginner?",[14,1232,1233],{},"Not at first, but a simple example helps you understand code you may see in real projects.",[291,1235,1237,1238,1240,1241,1244],{"id":1236},"why-use-show_before_after-instead-of-say_hello-show_before_aftersay_hello","Why use ",[67,1239,465],{}," instead of ",[67,1242,1243],{},"say_hello = show_before_after(say_hello)","?",[14,1246,1247,1248,1250],{},"They do the same thing. The ",[67,1249,177],{}," syntax is shorter and easier to read.",[291,1252,1254],{"id":1253},"why-does-my-decorator-fail-when-my-function-has-parameters","Why does my decorator fail when my function has parameters?",[14,1256,1257,1258,771,1260,54],{},"Your wrapper probably does not accept arguments. Use a wrapper that accepts ",[67,1259,770],{},[67,1261,774],{},[26,1263,1265],{"id":1264},"see-also","See also",[34,1267,1268,1272,1276,1281,1286],{},[37,1269,1270],{},[50,1271,53],{"href":52},[37,1273,1274],{},[50,1275,580],{"href":579},[37,1277,1278],{},[50,1279,1280],{"href":584},"How to create a simple function in Python",[37,1282,1283],{},[50,1284,1285],{"href":283},"What is a function in Python?",[37,1287,1288],{},[50,1289,1291],{"href":1290},"\u002Fglossary\u002Fwhat-is-a-method-in-python\u002F","What is a method in Python?",[1293,1294,1295],"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 .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 .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 .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 .stp6e, html code.shiki .stp6e{--shiki-light:#39ADB5;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}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}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":65,"searchDepth":95,"depth":95,"links":1297},[1298,1299,1300,1301,1314,1315,1316,1317,1318,1328,1335],{"id":28,"depth":95,"text":29},{"id":57,"depth":95,"text":58},{"id":255,"depth":95,"text":256},{"id":288,"depth":95,"text":289,"children":1302},[1303,1304,1306,1307,1308,1309,1311,1313],{"id":293,"depth":107,"text":294},{"id":323,"depth":107,"text":1305},"2. Define wrapper() inside the decorator",{"id":347,"depth":107,"text":348},{"id":374,"depth":107,"text":375},{"id":408,"depth":107,"text":409},{"id":434,"depth":107,"text":1310},"6. Return wrapper",{"id":461,"depth":107,"text":1312},"7. Apply the decorator with @show_before_after",{"id":555,"depth":107,"text":556},{"id":588,"depth":95,"text":589},{"id":618,"depth":95,"text":619},{"id":659,"depth":95,"text":660},{"id":780,"depth":95,"text":781},{"id":897,"depth":95,"text":898,"children":1319},[1320,1322,1324,1325,1326],{"id":904,"depth":107,"text":1321},"Forgetting to return wrapper",{"id":991,"depth":107,"text":1323},"Forgetting to call func() inside wrapper",{"id":1064,"depth":107,"text":1065},{"id":1116,"depth":107,"text":1117},{"id":1192,"depth":107,"text":1327},"Mixing up func and func()",{"id":1218,"depth":95,"text":1219,"children":1329},[1330,1331,1332,1334],{"id":1222,"depth":107,"text":1223},{"id":1229,"depth":107,"text":1230},{"id":1236,"depth":107,"text":1333},"Why use @show_before_after instead of say_hello = show_before_after(say_hello)?",{"id":1253,"depth":107,"text":1254},{"id":1264,"depth":95,"text":1265},"Master python decorator example simple use case in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-decorator-example-simple-use-case",{"title":5,"description":1336},"examples\u002Fpython-decorator-example-simple-use-case","CXbLtx7v6rfSKdy91M5n-hwct34tOD8BBsSEyn1kaR8",1777585475010]