python2和python3之间的可移植元类

科布拉斯

我正在尝试在python3中使用python2程序,它具有以下Meta类定义。在Py2上效果很好。使其与py2和py3都兼容的“最佳”方法是什么?

它在单元测试中失败了:

try:
    raise Actor.DoesNotExist
except Actor.DoesNotExist:
    pass

失败是:

AttributeError: type object 'Actor' has no attribute 'DoesNotExist'

基本元类定义为:

class MetaDocument(type):
    def __new__(meta,name,bases,dct):

        class DoesNotExist(BaseException):
            pass

        class MultipleDocumentsReturned(BaseException):
            pass
        dct['DoesNotExist'] = DoesNotExist
        dct['MultipleDocumentsReturned'] = MultipleDocumentsReturned
        class_type = type.__new__(meta, name, bases, dct)
        if not class_type in document_classes:
            if name == 'Document' and bases == (object,):
                pass
            else:
                document_classes.append(class_type)
        return class_type

class Document(object):
    __metaclass__ = MetaDocument
马丁·彼得斯(Martijn Pieters)

您可以将MetaDocument()元类用作工厂来产生替换您的Document类的类,从而重新使用类属性:

class Document(object):
    # various and sundry methods and attributes

body = vars(Document).copy()
body.pop('__dict__', None)
body.pop('__weakref__', None)

Document = MetaDocument(Document.__name__, Document.__bases__, body)

这不需要您手动构建第三个参数,即类主体。

您可以将其变成类装饰器:

def with_metaclass(mcls):
    def decorator(cls):
        body = vars(cls).copy()
        # clean out class body
        body.pop('__dict__', None)
        body.pop('__weakref__', None)
        return mcls(cls.__name__, cls.__bases__, body)
    return decorator

然后用作:

@with_metaclass(MetaDocument)
class Document(object):
    # various and sundry methods and attributes

或者,为此使用six

@six.add_metaclass(MetaDocument)
class Document(object):

其中,@six.add_metaclass()装饰也需要的任何照顾__slots__你可能已经定义; 我上面的简单版本没有。

six还有一个six.with_metaclass()基层工厂

class Document(six.with_metaclass(MetaDocument)):

这为MRO注入了额外的基类。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python2和python3之间的可移植元类

来自分类Dev

捕获标准输出时python2和python3之间的StringIO可移植性

来自分类Dev

Python2和Python3之间的字符编码

来自分类Dev

“ TypeError:object()不带参数”将python2元类转换为python3

来自分类Dev

Python3和Python2之间的区别-socket.send数据

来自分类Dev

使用元类将Python2转换为Python3会导致错误的流程

来自分类Dev

python2和python3中dict的__repr __()

来自分类Dev

正确处理Python2和Python3

来自分类Dev

正确处理Python2和Python3

来自分类Dev

可移植代码:Python 2和Python 3之间的__import__参数字符串类型

来自分类Dev

可移植代码:Python 2和Python 3之间的__import__参数字符串类型

来自分类Dev

Python2和Python3:__init__和__new__

来自分类Dev

Python3和pip3 +为python2安装的轮子也可以用于python3

来自分类Dev

使用python2和python3在stderr上编写的函数

来自分类Dev

以与python2和python3兼容的方式将字节写入标准输出

来自分类Dev

select()在python2和python3上的行为是否有所不同?

来自分类Dev

使用python2和python3创建一个virtualenv

来自分类Dev

在Python2和Python3中编写不同的十六进制值

来自分类Dev

如何分别为python3和python2设置不同的PYTHONPATH变量

来自分类Dev

Ansible json_query在python3和python2中的不同结果

来自分类Dev

为什么Python2和Python3的行为不同

来自分类Dev

使用 python3 和 python2 构建分发包的区别?

来自分类Dev

使用多个 Pipfiles 支持 python2 和 python3

来自分类Dev

Python2 和 Python3 中的 bytes.decode()

来自分类Dev

SublimeREPL for Python在Sublime Text 3和可移植Python的可移植版本上?

来自分类Dev

python2和3之间带有前导零的数字之间的差异。

来自分类Dev

Bottle mishandling JSON data in Python3, but not Python2

来自分类Dev

python2中的python3 datetime.timestamp?

来自分类Dev

在终端中用python3替换python2?

Related 相关文章

  1. 1

    python2和python3之间的可移植元类

  2. 2

    捕获标准输出时python2和python3之间的StringIO可移植性

  3. 3

    Python2和Python3之间的字符编码

  4. 4

    “ TypeError:object()不带参数”将python2元类转换为python3

  5. 5

    Python3和Python2之间的区别-socket.send数据

  6. 6

    使用元类将Python2转换为Python3会导致错误的流程

  7. 7

    python2和python3中dict的__repr __()

  8. 8

    正确处理Python2和Python3

  9. 9

    正确处理Python2和Python3

  10. 10

    可移植代码:Python 2和Python 3之间的__import__参数字符串类型

  11. 11

    可移植代码:Python 2和Python 3之间的__import__参数字符串类型

  12. 12

    Python2和Python3:__init__和__new__

  13. 13

    Python3和pip3 +为python2安装的轮子也可以用于python3

  14. 14

    使用python2和python3在stderr上编写的函数

  15. 15

    以与python2和python3兼容的方式将字节写入标准输出

  16. 16

    select()在python2和python3上的行为是否有所不同?

  17. 17

    使用python2和python3创建一个virtualenv

  18. 18

    在Python2和Python3中编写不同的十六进制值

  19. 19

    如何分别为python3和python2设置不同的PYTHONPATH变量

  20. 20

    Ansible json_query在python3和python2中的不同结果

  21. 21

    为什么Python2和Python3的行为不同

  22. 22

    使用 python3 和 python2 构建分发包的区别?

  23. 23

    使用多个 Pipfiles 支持 python2 和 python3

  24. 24

    Python2 和 Python3 中的 bytes.decode()

  25. 25

    SublimeREPL for Python在Sublime Text 3和可移植Python的可移植版本上?

  26. 26

    python2和3之间带有前导零的数字之间的差异。

  27. 27

    Bottle mishandling JSON data in Python3, but not Python2

  28. 28

    python2中的python3 datetime.timestamp?

  29. 29

    在终端中用python3替换python2?

热门标签

归档