[Python-Dev] Re: Useful thread project for 2.5?

Michael Chermside mcherm at mcherm.com
Tue Mar 8 15:55:52 CET 2005


Greg writes:
> This is one of my top two Java features [1].
          ...
> [1] The other is compiler recognition of "@deprecated" in doc comments.

Hmm... what a good idea! Let's see... what exactly does "@deprecated" DO in
Java? Why it causes the compiler to emit a warning if you use the function in
question. Not a bad idea. Amusingly enough, the syntax is even the same in
Python:


===== code =====
import warnings

def deprecated(func):
    """This is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emmitted
    when the function is used."""
    def newFunc(*args, **kwargs):
        warnings.warn("Call to deprecated function.")
        return func(*args, **kwargs)
    return newFunc


===== example =====
>>> @deprecated
def func(x,y):
	return x + y

>>> func(3,4)

Warning (from warnings module):
  File "g:/Documents/Personal/Python/deprecated.py", line 10
    warnings.warn("Call to deprecated function.")
UserWarning: Call to deprecated function.
7


So... shall I go add this to the cookbook?

-- Michael Chermside



More information about the Python-Dev mailing list