__metaclass__在Python 3中

发布于 2021-01-29 19:31:47

在Python2.7这个代码可以很好地工作,__getattr__MetaTable 运行。但是在Python 3中,它不起作用。

class MetaTable(type):
    def __getattr__(cls, key):
        temp = key.split("__")
        name = temp[0]
        alias = None

        if len(temp) > 1:
            alias = temp[1]

        return cls(name, alias)


class Table(object):
    __metaclass__ = MetaTable

    def __init__(self, name, alias=None):
        self._name = name
        self._alias = alias


d = Table
d.student__s

但是在Python 3.5中,我得到了一个属性错误:

Traceback (most recent call last):
  File "/Users/wyx/project/python3/sql/dd.py", line 31, in <module>
    d.student__s
AttributeError: type object 'Table' has no attribute 'student__s'
关注者
0
被浏览
41
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    Python
    3更改了您指定元类的方式__metaclass__不再检查。

    使用metaclass=...在类 签名

    class Table(object, metaclass=MetaTable):
    

    演示:

    >>> class MetaTable(type):
    ...     def __getattr__(cls, key):
    ...         temp = key.split("__")
    ...         name = temp[0]
    ...         alias = None
    ...         if len(temp) > 1:
    ...             alias = temp[1]
    ...         return cls(name, alias)
    ...
    >>> class Table(object, metaclass=MetaTable):
    ...     def __init__(self, name, alias=None):
    ...         self._name = name
    ...         self._alias = alias
    ...
    >>> d = Table
    >>> d.student__s
    <__main__.Table object at 0x10d7b56a0>
    

    如果需要在代码库中同时提供对Python
    2和3的支持,则可以使用six.with_metaclass()基类生成器@six.add_metaclass()类装饰器来指定元类。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看