훌륭한 장식자를 쓰는 것도 좋은 기능을 쓰는 것과 다르지 않습니다. 즉, 문서화 문자열을 사용하고 데코레이터가 테스트 프레임 워크에 포함되어 있는지 확인하는 것이 이상적입니다.
표준 라이브러리에 decorator
라이브러리 또는 더 나은 functools.wraps()
데코레이터를 사용해야합니다 (2.5 이후).
그 외에도 데코레이터의 초점을 맞추고 잘 디자인 된 상태로 유지하는 것이 가장 좋습니다. 데코레이터가 특정 인수를 예상하는 경우 *args
또는 **kw
을 사용하지 마십시오.
def keep_none(func):
def _exec(*args, **kw):
return None if args[0] is None else func(*args, **kw)
return _exec
... 사용 ...
def keep_none(func):
"""Wraps a function which expects a value as the first argument, and
ensures the function won't get called with *None*. If it is, this
will return *None*.
>>> def f(x):
... return x + 5
>>> f(1)
6
>>> f(None) is None
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>> f = keep_none(f)
>>> f(1)
6
>>> f(None) is None
True"""
@wraps(func)
def _exec(value, *args, **kw):
return None if value is None else func(value, *args, **kw)
return _exec
자신의 질문에 위키 답변을 위해 좋은 ettiquete : 그리고 당신이 기대하는 것을 인수, 그래서 대신에 칠을한다. – voyager
은 의미가 있습니다. – Casebash