I have a class, that has (too) many methods. Most of the methods take an argument (always the same name) – I need to change the type of the parameter to something else, but want to also accept the “old” version of the parameter (and raise a DeprecationWarning).
EDIT: We can assume that the argument is always passed as keyword argument.
What is the most DRY way of doing this?
The first solution that springs to my mind is something like:
def check_orc(orc):
if isinstance(Snaga, orc):
orc = convert_to_urukhai(orc)
raise DeprecationWarning("You should not be sending snaga to combat")
return orc
class Warrior():
def slash_orc(sword, shield, opponent):
opponent = check_orc(opponent)
...
def hack_orc(warhammer, opponent):
opponent = check_orc(opponent)
...
avasal’s comment is correct. For educational purposes however, here is a decorator implementation of what you want:
Note: I moved
opponentto the first position in the parameters