我们正在开发一个
python库,并希望改变某些函数中某些函数参数的命名方式.
我们希望保持向后兼容性,因此我们希望找到一种为函数参数创建别名的方法.
这是一个例子:
旧版:
class MyClass(object): def __init__(self,object_id): self.id = object_id
新版本:
class MyClass(object): def __init__(self,id_object): self.id = id_object
我们如何使类与两种调用方式兼容:
object1 = MyClass(object_id=1234) object2 = MyClass(id_object=1234)
我当然可以创建这样的东西:
class MyClass(object): def __init__(self,object_id=None,id_object=None): if id_object is not None: self.id = id_object else: self.id = object_id
但是,它会改变参数的数量,我们严格要避免这种情况.
有没有办法声明方法别名或参数别名?
解决方法
你可以写一个装饰者:
import functools import warnings def deprecated_alias(**aliases): def deco(f): @functools.wraps(f) def wrapper(*args,**kwargs): rename_kwargs(f.__name__,kwargs,aliases) return f(*args,**kwargs) return wrapper return deco def rename_kwargs(func_name,aliases): for alias,new in aliases.items(): if alias in kwargs: if new in kwargs: raise TypeError('{} received both {} and {}'.format( func_name,alias,new)) warnings.warn('{} is deprecated; use {}'.format(alias,new),DeprecationWarning) kwargs[new] = kwargs.pop(alias) class MyClass(object): @deprecated_alias(object_id='id_object') def __init__(self,id_object): self.id = id_object
或者,由于您使用的是Python 3,因此可以使object_id成为仅限关键字的参数:
import warnings class MyClass(object): # v Look here def __init__(self,id_object=None,*,object_id=None): if id_object is not None and object_id is not None: raise TypeError("MyClass received both object_id and id_object") elif id_object is not None: self.id = id_object elif object_id is not None: warnings.warn("object_id is deprecated; use id_object",DeprecationWarning) self.id = object_id else: raise TypeError("MyClass missing id_object argument")