best way to create warning for obsolete functions and call new one

Chris Angelico rosuav at
Mon Mar 26 23:50:35 CEST 2012

On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N <gelonida at> 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

You may want to try decorator syntax:

def was(oldname):
	def _(func):
		return func
	return _

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)
		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

def quux(spam,ham):

Hope that helps!


More information about the Python-list mailing list