Skip to content

Instantly share code, notes, and snippets.

@Gab-km Gab-km/
Last active Jul 31, 2017

What would you like to do?
C# の Obsolete 属性を Python に持ち込んでみた。
def obsolete(message=None, deprecated=False):
@message 警告に表示するテキスト
@deprecated すでに非推奨の場合、True
def outer(fn):
import warnings
def inner(*args, **kwargs):
if deprecated:
warnings.warn(message, DeprecationWarning, stacklevel=3)
warnings.warn(message, PendingDeprecationWarning, stacklevel=3)
return fn(*args, **kwargs)
return inner
return outer
import warnings
warnings.simplefilter("default") # これを実行するか、インタプリタ起動オプションに `-Wd` を渡すと、警告が表示されるようになる。
from .obsolete import obsolete
@obsolete("This function will be obsolete in the future.")
def old_add(x, y):
return x + y
print(old_add(1, 2))
#=> ~/src/py/ PendingDeprecationWarning: This function will be obsolete in the future.
# 3
@obsolete("This function is now deprecated.", True)
def deprecated_add(x, y):
return x + y
print(deprecated_add(2, 3))
#=> ~/src/py/ DeprecationWarning: This function is now deprecated.
# 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.