Vijeth Aradhya
Vijeth Aradhya

Reputation: 278

Creating a new function with lesser arguments by wrapping the old function on it

Suppose I have an old function, oldFunction(first, second, third). And, all the arguments are mandatory. I want to deprecate the use of 'second' argument, and also not change the code anywhere in my project.

I want to create a new function which does exactly the same thing, with only the first and the third argument.

I was thinking of wrapping the new function with new signature with old function as the wrapper. How can I possibly do that? Hence I will be able to use the new function with only two arguments, but when called by the old name, will the new function be called (because it is wrapped)?

How can I do that?

Upvotes: 2

Views: 137

Answers (2)

PotatoBox
PotatoBox

Reputation: 608

Why not use keyword arguments? This way you could have both:

def function(a, b, c=None):
    pass

And you can call it function(a, b) and also function(a, b, c).

Upvotes: 0

John Coleman
John Coleman

Reputation: 51998

Once you have newFunction(x,y) defined you can redefine oldFunction as:

def oldFunction(x,y,z):
    return newFunction(x,z)

Upvotes: 2

Related Questions