best way to create warning for obsolete functions and call new one
Chris Angelico
rosuav at gmail.com
Mon Mar 26 17:50:35 EDT 2012
On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N <gelonida at gmail.com> wrote:
> One option I though of would be:
>
> def obsolete_func(func):
> def call_old(*args, **kwargs):
> print "func is old psl use new one"
> return func(*args, **kwargs)
> return call_old
>
> and
>
> def get_time(a='high'):
> return a + 'noon'
That's a reasonable idea. Incorporate Dan's suggestion of using
DeprecationWarning.
You may want to try decorator syntax:
def was(oldname):
def _(func):
globals()[oldname]=func
return func
return _
@was("get_thyme")
def get_time(a='high'):
return a + 'noon'
That won't raise DeprecationWarning, though. It's a very simple
assignment. The was() internal function could be enhanced to do a bit
more work, but I'm not sure what version of Python you're using and
what introspection facilities you have. But if you're happy with the
old versions coming up with (*args,**kwargs) instead of their
parameter lists, it's not difficult:
def was(oldname):
def _(func):
def bounce(*args,**kwargs):
# raise DeprecationWarning
return func(*args,**kwargs)
globals()[oldname]=bounce
return func
return _
I've never actually used the Python warnings module, but any line of
code you fill in at the comment will be executed any time the old name
is used. In any case, it's still used with the same convenient
decorator. You could even unify multiple functions under a single new
name:
@was("foo")
@was("bar")
def quux(spam,ham):
return ham.eat()
Hope that helps!
ChrisA
More information about the Python-list
mailing list