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

Gelonida N gelonida at
Wed Mar 28 09:51:52 CEST 2012

Hi Chris,

On 03/26/2012 11:50 PM, Chris Angelico wrote:
> 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
> DeprecationWarning.
Will do that.

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

I'm using python 2.6 and sometimes still 2.5 (the latter is not
important for this question though)

Good idea about the decorators.
I overlooked to see, that if done properly a decorator will not add call
time overhead for calling the function with it's new name

> 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
Now the next step will do write a decorator working for class methods.
I think I have a solution, but it would require to pass the class as
additional parameter to each decorator.

it would be
setattr(cls, oldname, finc)

> Hope that helps!
It does. Thanks again.

More information about the Python-list mailing list