Monostate __new__ 的 Python 弃用警告 - 有人能解释一下原因吗?

发布于 2024-08-08 04:11:21 字数 1028 浏览 1 评论 0原文

我有一个带有 Python 2.6 的基本 Monostate。

class Borg(object):
    __shared_state = {}
    def __new__(cls, *args, **kwargs):
        self = object.__new__(cls, *args, **kwargs)
        self.__dict__ = cls.__shared_state
        return self

    def __init__(self, *args, **kwargs):
        noSend = kwargs.get("noSend", False)
        reportLevel = kwargs.get("reportLevel", 30)
        reportMethods = kwargs.get("reportMethods", "BaseReport")
        contacts= kwargs.get("contacts", None)

a = Borg(contacts="Foo", noSend="Bar", )

这很高兴地给了我以下弃用警告。

untitled:4: DeprecationWarning: object.__new__() takes no parameters
  self = object.__new__(cls, *args, **kwargs)

经过一番谷歌搜索后,我发现它附加到 Bug #1683368。我不明白这意味着什么。它抱怨以下行

self = object.__new__(cls, *args, **kwargs)

似乎没问题。有人可以用外行的术语解释一下为什么这是一个问题。我理解“这与其他内置函数不一致,例如列表”,但我不确定我是否理解原因。有人能解释一下这告诉我正确的方法吗?

谢谢

I have a basic Monostate with Python 2.6.

class Borg(object):
    __shared_state = {}
    def __new__(cls, *args, **kwargs):
        self = object.__new__(cls, *args, **kwargs)
        self.__dict__ = cls.__shared_state
        return self

    def __init__(self, *args, **kwargs):
        noSend = kwargs.get("noSend", False)
        reportLevel = kwargs.get("reportLevel", 30)
        reportMethods = kwargs.get("reportMethods", "BaseReport")
        contacts= kwargs.get("contacts", None)

a = Borg(contacts="Foo", noSend="Bar", )

Which happily gives me the following Deprecation warning..

untitled:4: DeprecationWarning: object.__new__() takes no parameters
  self = object.__new__(cls, *args, **kwargs)

After a bit of googling I find this is attached to Bug #1683368. What I can't figure out is what this means. It's complaining about the following line

self = object.__new__(cls, *args, **kwargs)

Which appears to be OK. Can someone explain in laymens terms why this is a problem. I understand that "this is inconsistent with other built-ins, like list" but I'm not sure I understand why. Would someone explain this show me the right way to do it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-08-15 04:11:21

请参阅 python-singleton-object-instantiation,并注意 Alex Martelli 的 单例示例:

class Singleton(object):

    __instance = None

    def __new__(cls):
        if cls.__instance == None:
            __instance = type.__new__(cls)
            __instance.name = "The one"
        return __instance

__new__ 弃用 问题是 Guido 回答

该消息的意思就是它所说的。 :-) 打电话没有意义
object.__new__() 具有多个类参数,以及任何代码
这样做只是将这些参数扔进黑洞。

object.__new__() 忽略额外内容的唯一一次有意义
参数是当它没有被覆盖,但 __init__ is 被覆盖时
重写——那么你就有了一个完全默认的 __new__ 和
构造函数参数的检查被降级为 __init__。

这一切的目的是捕获像这样的调用中的错误
object(42)(再次)传递一个未使用的参数。这是
通常是程序中存在错误的症状。

--吉多

See python-singleton-object-instantiation, and note Alex Martelli's singleton example:

class Singleton(object):

    __instance = None

    def __new__(cls):
        if cls.__instance == None:
            __instance = type.__new__(cls)
            __instance.name = "The one"
        return __instance

The __new__ deprecation question was answered by Guido:

The message means just what it says. :-) There's no point in calling
object.__new__() with more than a class parameter, and any code that
did so was just dumping those args into a black hole.

The only time when it makes sense for object.__new__() to ignore extra
arguments is when it's not being overridden, but __init__ is being
overridden -- then you have a completely default __new__ and the
checking of constructor arguments is relegated to __init__.

The purpose of all this is to catch the error in a call like
object(42) which (again) passes an argument that is not used. This is
often a symptom of a bug in your program.

--Guido

挽容 2024-08-15 04:11:21

该警告来自这样一个事实:__new__() 可以有参数,但由于它们在任何地方都被忽略,因此将参数(除了 cls)传递给它会导致警告。传递额外参数实际上(目前)并不是一个错误,但它们没有任何效果。

在 py3k 中,传递 args 会出错。

The warning comes from the fact that __new__() can HAVE args, but since they're ignored everywhere, passing args (other than cls) to it causes the warning. It's not actually (currently) an error to pass the extra args, but they have no effect.

In py3k it will become an error to pass the args.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文