I am trying to use the following decorator on functions defined in or imported to iPython notebook:
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 emitted
when the function is used.'''
def new_func(*args, **kwargs):
warnings.warn("Call to deprecated function {}.".format(func.__name__),
category=DeprecationWarning)
return func(*args, **kwargs)
new_func.__name__ = func.__name__
new_func.__doc__ = func.__doc__
new_func.__dict__.update(func.__dict__)
return new_func
I defined the decorator in utils.py
. When I use the decorator this way:
import utils #from utils import deprecated
@utils.deprecated
def test():
print 'Brokolice'
Then running test()
prints 'Brokolice', but does not give any warning. However, when I define the decorator within iPython, I get the desired deprecated warning.
I am using Python 2.7 and I am not yet very comfortable with decorators or Python in general, but in this case, I have no idea what is wrong, as I would expect some kind of error if import of the decorator failed.