微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用参数别名在python中实现弃用

我们正在开发一个 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")

原文地址:https://www.jb51.cc/python/186630.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐