gpt4 book ai didi

python - _replace 不调用命名元组子类的 __new__

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:16 25 4
gpt4 key购买 nike

我有一个命名元组类型的子类:

class User(namedtuple('User', ['first_name'])):
__slots__ = ()

def __new__(cls, *args, **kwargs):
result = super().__new__(cls, *args, **kwargs)
if not result.first_name:
raise InvalidUserError({InvalidUserError.EMPTY_FIRST_NAME})
return result

创建新用户按预期工作:

>>> try: User(first_name='')
... except Exception as e: print(type(e))
<class 'InvalidUserError'>

但是,当使用 _replace 时,不会调用 __new__ 方法:

>>> User(first_name='foo')._replace(first_name='')
User(first_name='')

有没有办法保证namedtuple不变量?我正在使用 Python 3.4。

最佳答案

Python 总体上依赖于约定和良好的文档,而不是强烈强制执行不变量。即使没有 _replace(),您也可以绕过 User.__new__():

>>> class X(tuple): __slots__ = ()
>>> x = C(('',))
>>> x.__class__ = User
>>> x
User(first_name='')

所以不,您永远无法严格执行此操作。只需避免使用 _replace(),或使用调用 User.__new__() 的版本覆盖它,或检查不同级别的不变量。

_replace() 的示例实现:

def _replace(self, **kwargs):
return type(self)(**dict(vars(self), **kwargs))

关于python - _replace 不调用命名元组子类的 __new__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com