본문 바로가기
카테고리 없음

Primer on Python Decorators 데코레이터 입문서

by EasyGPT 2023. 12. 23.
반응형

Primer on Python Decorators

Python 데코레이터 입문서

by Geir Arne Hjelle

Aug 22, 2018

intermediate python

Table of Contents

데코레이터에 대한 이 튜토리얼에서는 데코레이터가 무엇인지, 어떻게 만들고 사용하는지 살펴보겠습니다.

데코레이터는 고차함수 higher-order functions 호출을 위한 간단한 구문을 제공.

정의에 따르면 데코레이터는 다른 함수를 취하고 명시적으로 수정하지 않고 후자 함수의 동작을 확장하는 함수.

혼란스럽게 들리지만 실제로는 그렇지 않습니다.

특히 데코레이터가 작동하는 방식에 대한 몇 가지 예를 본 후에는 더욱 그렇습니다.

여기에서 이글의 모든 예제를 찾을 수 있습니다.

In this tutorial on decorators, we’ll look at what they are and how to create and use them. Decorators provide a simple syntax for calling higher-order functions.

By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

This sounds confusing, but it’s really not, especially after you’ve seen a few examples of how decorators work. You can find all the examples from this article here.

Updates:

  • 08/22/2018: Major update adding more examples and more advanced decorators
  • 01/12/2016: Updated examples to Python 3 (v3.5.1) syntax and added a new example
  • 11/01/2015: Added a brief explanation on the functools.wraps() decorator

Functions

데코레이터를 이해하려면 먼저 함수 작동 방식을 이해해야 합니다.

목적에 맞게 함수는 주어진 인수를 기반으로 값을 반환.

Before you can understand decorators, you must first understand how functions work. For our purposes, a function returns a value based on the given arguments.

다음은 매우 간단한 예:

Here is a very simple example:

Python

>>> def add_one(number):

... return number + 1

>>> add_one(2)

3

일반적으로 Python의 함수는 입력을 출력으로 바꾸는 것 이상의 부작용이 있을 수도 있습니다.

print() function는 이에 대한 기본적 예: 콘솔에 무언가를 출력하는 부작용이 있으면 None을 반환 returns None.

그런데 데코레이터를 이해하려면, 함수를 주어진 인수를 값으로 바꾸는 것으로 생각하는 것만으로도 충분합니다.

In general, functions in Python may also have side effects rather than just turning an input into an output. The print() function is a basic example of this: it returns None while having the side effect of outputting something to the console. However, to understand decorators, it is enough to think about functions as something that turns given arguments into a value.

Note: functional programming에서는 (거의) 부작용 없이 순수한 함수로만 작업.

순수한 함수형 언어는 아니지만 Python은 일급 객체로서의 함수 등 많은 함수형 프로그래밍 개념을 지원.

In functional programming, you work (almost) only with pure functions without side effects. While not a purely functional language, Python supports many of the functional programming concepts, including functions as first-class objects.

일급 객체

First-Class Objects

Python에서 함수는 일급 객체.

즉, 모든 다른 객체 (string, int, float, list 등)와 마찬가지로 함수를 전달하고, 함수를 인수로 사용할 수 있습니다.

In Python, functions are first-class objects. This means that functions can be passed around and used as arguments, just like any other object (string, int, float, list, and so on).

다음 3가지 함수를 검토:

Consider the following three functions:

Python

def say_hello(name):

return f"Hello {name}"

def be_awesome(name):

return f"Yo {name}, together we are the awesomest!"

def greet_bob(greeter_func):

return greeter_func("Bob")

여기서 say_hello()와 be_awesome()문자열 string로 주어진 이름을 기대하는 일반함수.

그러나 greet_bob() 함수는 인수로 함수(greeter_func)를 기대.

예를 들어, say_hello() 또는 be_awesome() 함수를 이 함수에인수로 전달할 수 있습니다.

Here, say_hello() and be_awesome() are regular functions that expect a name given as a string. The greet_bob() function however, expects a function as its argument. We can, for instance, pass it the say_hello() or the be_awesome() function:

Python

>>> greet_bob(say_hello)

'Hello Bob'

>>> greet_bob(be_awesome)

'Yo Bob, together we are the awesomest!'

greet_bob(say_hello)는 2개 함수(greet_bob()과 say_hello)를 참조하지만, 그 방식은 서로 다름.

say_hello 함수는 괄호 없이 이름이 지정됩니다.

즉, 함수에 대한 참조만 전달됩니다.

함수는 실행되지 않습니다.

반면, greet_bob() 함수괄호와 함께 작성되어 있어 평소대로 호출됨.

Note that greet_bob(say_hello) refers to two functions, but in different ways: greet_bob() and say_hello. The say_hello function is named without parentheses. This means that only a reference to the function is passed. The function is not executed. The greet_bob() function, on the other hand, is written with parentheses, so it will be called as usual.

def say_hello(name):

return f"Hello {name}"

 

print(say_hello("Lee"))

 

def greet_bob(say_hello):

return say_hello("KimBob")

print(greet_bob(say_hello))

def greet_bob(greeter_func):

return greeter_func("Bob")

def greeter_func(name):

return f"Hi {name}"

 

print(greeter_func("Fang"))

print(greet_bob(greeter_func))

Output

Hello Lee

Hello KimBob

Hi Fang

Hi Bob

Inner Functions

내부 함수

inside other functions에서 함수를 정의하는 것이 가능합니다.

이러한 함수를 inner functions라고 합니다.

It’s possible to define functions inside other functions. Such functions are called inner functions.

다음은 2개의 내부함수가 있는 함수의 예:

Here’s an example of a function with two inner functions:

Python

def parent():

print("Printing from the parent() function")

def first_child():

print("Printing from the first_child() function")

def second_child():

print("Printing from the second_child() function")

parent()

first_child()

second_child()

Output

Printing from the parent() function

Printing from the first_child() function

Printing from the second_child() function

def parent():

print("Printing from the parent() function")

 

def first_child():

print("Printing from the first_child() function")

def second_child():

print("Printing from the second_child() function")

second_child()

first_child()

내부함수가 정의되는 순서는 중요하지 않습니다.

다른 함수와 마찬가지로, printing는 내부함수가 실행될 때만 발생합니다.

Note that the order in which the inner functions are defined does not matter. Like with any other functions, the printing only happens when the inner functions are executed.

또한 내부함수는 상위함수가 호출될 때까지 정의되지 않습니다.

내부함수는 로컬 범위 parent()로 로컬로 범위가 지정됩니다: 내부함수는 parent() 함수 내부에서 로컬 variables로만 존재합니다.

Furthermore, the inner functions are not defined until the parent function is called. They are locally scoped to parent(): they only exist inside the parent() function as local variables.

first_child()를 호출해 보세요.

Try calling first_child().

다음과 같은 오류가 발생:

You should get an error:

Python Traceback

Output

Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'first_child' is not defined

메시지=name 'first_child' is not defined

소스=C:\Users\user\PythonApp\PythonApp.py

StackTrace:

File "C:\Users\user\PythonApp\PythonApp.py", line 10, in <module> (Current frame)

first_child()

^^^^^^^^^^^

NameError: name 'first_child' is not defined

parent()를 호출할 때마다 내부함수 first_child() 및 second_child()도 호출됩니다. 그러나 로컬 범위로 인해 parent() 함수 외부에서는 사용할 수 없습니다.

Whenever you call parent(), the inner functions first_child() and second_child() are also called. But because of their local scope, they aren’t available outside of the parent() function.

https://amzn.to/3RSUXmd

 
 

함수로부터 함수 반환

Returning Functions From Functions

Python에서는 함수를 반환 값으로 사용할 수도 있습니다.

다음 예에서는 외부함수에서 내부함수 중 하나를 반환:

Python also allows you to use functions as return values. The following example returns one of the inner functions from the outer parent() function:

Python

def parent(num):

def first_child():

return "Hi, I am Emma"

def second_child():

return "Call me Liam"

if num == 1:

return first_child

else:

return second_child

print(parent(1))

Output

<function parent.<locals>.first_child at 0x00000207B4430A40>

print(parent(100))

Output

<function parent.<locals>.second_child at 0x0000022444364F40>

first_child괄호없이 반환한다는 점에 유의하세요.

이는 first_child 함수에 대한 참조를 반환한다는 의미임을 기억하세요.

Note that you are returning first_child without the parentheses. Recall that this means that you are returning a reference to the function first_child.

대조적으로 괄호있는 first_child()는 함수 평가결과를 나타냅니다.

이는 다음 예에서 볼 수 있습니다.

In contrast first_child() with parentheses refers to the result of evaluating the function. This can be seen in the following example:

Python

>>> first = parent(1)

>>> second = parent(2)

>>> first <function parent.<locals>.first_child at 0x7f599f1e2e18>

>>> second <function parent.<locals>.second_child at 0x7f599dad5268>

다소 비밀스러운 출력은 단순히

첫째 변수가 parent() 내부의 로컬 first_child() 함수를 참조하고, 두 번째 변수가 second_child()를 가리킨다는 것을 의미.

The somewhat cryptic output simply means that the first variable refers to the local first_child() function inside of parent(), while second points to second_child().

def parent(num):

def first_child():

return "Hi, I am Emma"

def second_child():

return "Call me Liam"

if num == 1:

return first_child

else:

return second_child

 

first = parent(1)

second = parent(2)

print(first)

print(second)

 

print(parent(1))

print(parent(2))

first()

print(first())

second()

print(second())

Output

<function parent.<locals>.first_child at 0x000001DCA7050A40>

<function parent.<locals>.second_child at 0x000001DCA7050CC0>

<function parent.<locals>.first_child at 0x000001DCA7050F40>

<function parent.<locals>.second_child at 0x000001DCA70511C0>

Hi, I am Emma

Call me Liam

이제 첫째와 두 번째 함수가 가리키는 함수에 직접 액세스할 수 없더라도 일반함수인 것처럼 사용할 수 있습니다.

You can now use first and second as if they are regular functions, even though the functions they point to can’t be accessed directly:

Python

>>> first()

'Hi, I am Emma'

>>> second()

'Call me Liam'

마지막으로 이전 예제에서는 부모함수 내에서 내부함수(예: first_child())를 실행했습니다.

그러나 이 마지막 예에서는 반환 시 내부함수(first_child)에 괄호를 추가하지 않았습니다.

이렇게 하면 나중에 호출할 수 있는 각 함수에 대한 참조를 얻을 수 있습니다. 말이 되나요?

Finally, note that in the earlier example you executed the inner functions within the parent function, for instance first_child(). However, in this last example, you did not add parentheses to the inner functions—first_child—upon returning. That way, you got a reference to each function that you could call in the future. Make sense?

Simple Decorators

이제 함수가 Python의 다른 객체와 똑같다는 것을 알았으므로 계속해서 Python 데코레이터라는 마법의 짐승을 볼 준비가 되었습니다:

Now that you’ve seen that functions are just like any other object in Python, you’re ready to move on and see the magical beast that is the Python decorator. Let’s start with an example:

Python

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.")

return wrapper

def say_whee():

print("Whee!")

say_whee = my_decorator(say_whee)

say_whee()를 호출하면 무슨 일이 일어날지 추측할 수 있나요?

시도해보세요:

Can you guess what happens when you call say_whee()? Try it:

Python

>>> say_whee()

Something is happening before the function is called.

Whee!

Something is happening after the function is called.

여기서 무슨 일이 일어나고 있는지 이해하려면 이전 예를 다시 살펴보세요.

우리는 지금까지 배운 모든 것을 문자 그대로 적용하고 있습니다.

소위 decoration은 다음 줄에서 발생:

To understand what’s going on here, look back at the previous examples. We are literally just applying everything you have learned so far.

The so-called decoration happens at the following line:

Python

say_whee = my_decorator(say_whee)

실제로, 이름 say_whee는 이제 wrapper() inner 함수를 가리킵니다.

my_decorator(say_whee)를 호출할 때 wrapper를 함수로 반환한다는 점을 기억하세요.

In effect, the name say_whee now points to the wrapper() inner function. Remember that you return wrapper as a function when you call my_decorator(say_whee):

Python

>>> say_whee

<function my_decorator.<locals>.wrapper at 0x7f3c5dfd42f0>

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.")

return wrapper

def say_whee():

print("Whee!")

 

print(say_whee)

say_whee = my_decorator(say_whee)

say_whee()

Output

Something is happening before the function is called.

Whee!

<function my_decorator.<locals>.wrapper at 0x000001FC38C51080>

Something is happening after the function is called.

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.")

return wrapper

def say_whee():

print("Whee!")

say_whee = my_decorator(say_whee)

say_whee()

print(say_whee)

Output

Something is happening before the function is called.

Whee!

Something is happening after the function is called.

<function my_decorator.<locals>.wrapper at 0x000001A2D1F31080>

그러나 wrapper()는 원본 say_whee()에 대한 참조를 func로 갖고 있으며, print()에 대한 두 호출 사이에 해당 함수를 호출합니다.

However, wrapper() has a reference to the original say_whee() as func, and calls that function between the two calls to print().

간단히 말하면: 데코레이터는 함수를 래핑하여, 동작을 수정합니다.

Put simply: decorators wrap a function, modifying its behavior.

계속 진행하기 전에 두 번째 예를 살펴보겠습니다.

wrapper()는 일반 Python 함수이므로 데코레이터가 함수를 수정하는 방식이 동적으로 변경될 수 있습니다.

다음 예제에서는, 이웃에 방해되지 않도록 낮에만 데코레이팅된 코드를 실행:

Before moving on, let’s have a look at a second example. Because wrapper() is a regular Python function, the way a decorator modifies a function can change dynamically. So as not to disturb your neighbors, the following example will only run the decorated code during the day:

Python

from datetime import datetime

def not_during_the_night(func):

def wrapper():

if 7 <= datetime.now().hour < 22:

func()

else:

pass # Hush, the neighbors are asleep

return wrapper

def say_whee():

print("Whee!")

say_whee = not_during_the_night(say_whee)

취침시간에 say_whee()를 호출하려 하면, 아무 일도 일어나지 않습니다:

If you try to call say_whee() after bedtime, nothing will happen:

Python

>>> say_whee()

>>>

 

from datetime import datetime

def not_during_the_night(func):

def wrapper():

if 7 <= datetime.now().hour < 22:

func()

else:

pass # Hush, the neighbors are asleep

 

return wrapper

def say_whee():

print("Whee!")

say_whee = not_during_the_night(say_whee)

print(say_whee)

say_whee()

Output

<function not_during_the_night.<locals>.wrapper at 0x0000021DB5001080>

Whee!

Syntactic Sugar!

위에서 say_whee()를 장식한 방식은 약간 투박합니다.

우선, say_whee라는 이름을 세 번 입력하게 됩니다.

게다가 장식은 함수 정의 아래에 약간 숨겨져 있습니다.

The way you decorated say_whee() above is a little clunky. First of all, you end up typing the name say_whee three times. In addition, the decoration gets a bit hidden away below the definition of the function.

대신 Python에서는 @ symbol (때때로 “pie” syntax)를 사용하여 더 간단한 방식으로 데코레이터를 사용할 수 있습니다.

Instead, Python allows you to use decorators in a simpler way with the @ symbol, sometimes called the “pie” syntax.

다음 예제는 첫째 데코레이터 예제와 똑같은 작업을 수행:

The following example does the exact same thing as the first decorator example:

Python

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.") return wrapper

@my_decorator

def say_whee():

print("Whee!")

따라서 @my_designator는 say_whee = my_designator(say_whee)를 더 쉽게 표현하는 방법입니다.

함수에 데코레이터를 적용하는 방법입니다.

So, @my_decorator is just an easier way of saying say_whee = my_decorator(say_whee). It’s how you apply a decorator to a function.

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.")

return wrapper

def say_whee():

print("Whee!")

say_whee = my_decorator(say_whee)

def my_decorator(func):

def wrapper():

print("Something is happening before the function is called.")

func()

print("Something is happening after the function is called.")

return wrapper

@my_decorator

def say_whee():

print("Whee!")

 

say_whee()

print(say_whee)

Output

Something is happening before the function is called.

Whee!

Something is happening after the function is called.

<function my_decorator.<locals>.wrapper at 0x00000237B8535080>

Reusing Decorators

데코레이터는 단지 일반 Python 함수일 뿐이라는 점을 기억하세요.

쉽게 재사용할 수 있는 일반적 도구는 모두 데코레이트할 수 있습니다.

다른 많은 함수에서 사용할 수 있는 자체 module로 데코레이터를 이동해 보겠습니다.

다음 내용으로 decorators.py라는 파일을 만듭니다.

Recall that a decorator is just a regular Python function. All the usual tools for easy reusability are available. Let’s move the decorator to its own module that can be used in many other functions.

Create a file called decorators.py with the following content:

Python

def do_twice(func):

def wrapper_do_twice():

func()

func()

return wrapper_do_twice

Note: 내부함수 이름은 원하는 대로 지정할 수 있으며 일반적으로 Wrapper() 같은 일반 이름을 사용하는 것이 좋습니다. 이 글에서는 많은 데코레이터를 보게 될 것입니다. 이들을 구별하기 위해 데코레이터와 동일한 이름이지만 래퍼_ 접두사를 사용하여 내부함수 이름을 지정.You can name your inner function whatever you want, and a generic name like wrapper() is usually okay. You’ll see a lot of decorators in this article. To keep them apart, we’ll name the inner function with the same name as the decorator but with a wrapper_ prefix.

이제 일반 import를 수행하여 다른 파일에서 이 새 데코레이터를 사용할 수 있슴:

You can now use this new decorator in other files by doing a regular import:

Python

from decorators import do_twice

@do_twice

def say_whee():

print("Whee!")

이 예제를 실행하면 원래 say_whee()가 두 번 실행되는 것을 볼 수 있습니다:

When you run this example, you should see that the original say_whee() is executed twice:

Python

>>> say_whee()

Whee!

Whee!

인수로 함수 꾸미기

Decorating Functions With Arguments

몇 가지 인수를 받아들이는 함수가 있다고 가정해 보겠습니다.

아직도 decorate할 수 있나요?

Say that you have a function that accepts some arguments. Can you still decorate it?

Python

from decorators import do_twice

@do_twice

def greet(name):

print(f"Hello {name}")

불행하게도 이 코드를 실행하면 오류가 발생합니다.

Unfortunately, running this code raises an error:

Python

>>> greet("World") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: wrapper_do_twice() takes 0 positional arguments but 1 was given

문제는 내부함수 wrapper_do_twice()가 인수를 취하지 않는데, name="World" 가 전달되었다는 것입니다.

wrapper_do_twice()1개의 인수를 허용하게 하여 이 문제를 해결할 수 있지만, 그러면 이전에 만든 say_whee() 함수에서는 작동하지 않습니다.

해결책은 내부 래퍼함수에 *args 및 **kwargs를 사용하는 것.

그런 다음 임의의 수의 위치 및 키워드 인수를 허용합니다.

decorators.py를 다음과 같이 다시 작성합니다:

The problem is that the inner function wrapper_do_twice() does not take any arguments, but name="World" was passed to it. You could fix this by letting wrapper_do_twice() accept one argument, but then it would not work for the say_whee() function you created earlier.

The solution is to use *args and **kwargs in the inner wrapper function. Then it will accept an arbitrary number of positional and keyword arguments. Rewrite decorators.py as follows:

Python

def do_twice(func):

def wrapper_do_twice(*args, **kwargs):

func(*args, **kwargs)

func(*args, **kwargs)

return wrapper_do_twice

wrapper_do_twice() 내부함수는 이제 임의 개수의 인수를 허용하고 이를 내부함수가 decorate하는 함수에 전달합니다.

이제 say_whee() 및 Greeting() 예제가 모두 작동합니다.

The wrapper_do_twice() inner function now accepts any number of arguments and passes them on to the function it decorates. Now both your say_whee() and greet() examples works:

Python

>>> say_whee()

Whee! Whee!

>>> greet("World")

Hello World Hello World

Returning Values From Decorated Functions

decorate된 함수에서의 값 반환

decorated 함수의 반환 값은 어떻게 되나요?

글쎄, 그건 데코레이터가 결정하는 몫.

다음과 같이 간단한 함수를 장식한다고 가정해 보겠습니다.

What happens to the return value of decorated functions? Well, that’s up to the decorator to decide. Let’s say you decorate a simple function as follows:

Python

from decorators import do_twice

@do_twice

def return_greeting(name):

print("Creating greeting")

return f"Hi {name}"

Try to use it:

Python

>>> hi_adam = return_greeting("Adam")

Creating greeting

Creating greeting

>>> print(hi_adam)

None

이런, 데코레이터가 함수의 반환 값을 먹었습니다.

do_twice_wrapper()가 명시적으로 값을 반환하지 않기 때문에 return_greeting("Adam") 호출은 결국 None을 반환하게 되었습니다.

이 문제를 해결하려면 래퍼함수가 데코레이팅된 함수의 반환 값을 반환하는지 확인해야 합니다.

decorators.py 파일을 변경하세요.

Oops, your decorator ate the return value from the function.

Because the do_twice_wrapper() doesn’t explicitly return a value, the call return_greeting("Adam") ended up returning None.

To fix this, you need to make sure the wrapper function returns the return value of the decorated function. Change your decorators.py file:

Python

def do_twice(func):

def wrapper_do_twice(*args, **kwargs):

func(*args, **kwargs)

return func(*args, **kwargs)

return wrapper_do_twice

마지막 함수 실행의 반환 값이 반환:

The return value from the last execution of the function is returned:

Python

>>> return_greeting("Adam")

Creating greeting

Creating greeting

'Hi Adam'

Who Are You, Really?

특히 대화형 셸에서 Python으로 작업할 때 매우 편리한 점은 강력한 내부 검사 기능입니다.

자체 검사는 런타임 시 객체가 자신의 속성을 알 수 있는 능력.

예를 들어, 함수는 자신의 이름과 문서를 알고 있습니다.

A great convenience when working with Python, especially in the interactive shell, is its powerful introspection ability. Introspection is the ability of an object to know about its own attributes at runtime. For instance, a function knows its own name and documentation:

Python

>>> print

<built-in function print>

>>> print.__name__

'print'

>>> help(print)

Help on built-in function print in module builtins: print(...)

<full help message>

자체 검사는 사용자가 직접 정의한 함수에도 적용됩니다.

The introspection works for functions you define yourself as well:

Python

>>> say_whee

<function do_twice.<locals>.wrapper_do_twice at 0x7f43700e52f0>

>>> say_whee.__name__

'wrapper_do_twice'

>>> help(say_whee)

Help on function wrapper_do_twice in module decorators: wrapper_do_twice()

그러나, say_whee()는 장식된 후에 그 정체성에 대해 매우 혼란스러워졌습니다.

이제 do_twice() 데코레이터 내부의 wrapper_do_twice() 내부함수임을 보고합니다.

기술적으로는 사실이지만 이는 그다지 유용한 정보는 아닙니다.

이 문제를 해결하려면 데코레이터는 원래 함수에 대한 정보를 보존하는 @functools.wraps 데코레이터를 사용해야 합니다.

decorators.py를 다시 업데이트하세요.

However, after being decorated, say_whee() has gotten very confused about its identity. It now reports being the wrapper_do_twice() inner function inside the do_twice() decorator. Although technically true, this is not very useful information.

To fix this, decorators should use the @functools.wraps decorator, which will preserve information about the original function. Update decorators.py again:

Python

import functools

def do_twice(func):

@functools.wraps(func)

def wrapper_do_twice(*args, **kwargs):

func(*args, **kwargs)

return func(*args, **kwargs)

return wrapper_do_twice

장식된 say_whee() 함수에 대해서는 아무것도 변경할 필요가 없슴:

You do not need to change anything about the decorated say_whee() function:

Python

>>> say_whee

<function say_whee at 0x7ff79a60f2f0>

>>> say_whee.__name__

'say_whee'

>>> help(say_whee)

Help on function say_whee in module whee: say_whee()

훨씬 좋습니다!

이제 say_whee()는 여전히 장식 이후 그 자체입니다.

Much better! Now say_whee() is still itself after decoration.

Technical Detail: @functools.wraps 데코레이터는 functools.update_wrapper() 함수를 사용하여 인트로스펙션에 사용되는 __name__ 및 __doc__ 같은 특수 속성을 업데이트합니다. The @functools.wraps decorator uses the function functools.update_wrapper() to update special attributes like __name__ and __doc__ that are used in the introspection.

반응형

댓글