问题描述
在阅读本文时:什么是Python中的元类?,我正在使用以下代码段学习使用__new__
:-
While reading this: What is a metaclass in Python?, I am learning to use __new__
using the following snippet:-
class a(object):
pass
a.__new__(int,'abcdef',(int,),{})
使用a.
调用__new__
可能存在一些问题.但是,我收到以下错误,但我不理解其含义:-
There could be some problem in calling __new__
using a.
. But, I get the following error, the meaning of which I do not understand:-
TypeError: object.__new__(int) is not safe, use int.__new__()
如果与__new__
的使用有关,我可以通过阅读一些书来加以补充.但是,有人可以解释为什么出现此消息吗?
If there is something to do with the usage of __new__
, I can ammend that by reading some books. But, can someone please explain why this message comes:
object.__new__(int) is not safe, use int.__new__()
推荐答案
简单地放入2.6之前的版本-object.__new__
被忽略的参数...现在它实际上可以对它们起作用.因此,警告是可能会发生一些不同的事情.
Put simply, prior to 2.6 - object.__new__
ignored arguments... Now it actually may do something with them... So the warning is that something different may happen.
此帖子有关以下内容的任何想法__new__失去论据的最佳解决方法?-有更详细的解释.
This post Any ideas about the best work around for __new__ losing its arguments? - has a much more detailed explanation.
您可能还希望查看 http://docs.python.org /library/functions.html#type
知道Python中所有 meta 的功能都是很棒的,但是作为警告,我不会对此太挂念-因为我已经看到了一些非常聪明"的东西解决方案,那只是怪兽...
Knowing what's possible with all the meta stuff in Python is great, but just as a warning, I wouldn't get too hung up about it - as I've seen some really "clever" solutions, that are just monstrosities...
这篇关于TypeError:object .__ new __(int)不安全,请使用int .__ new __()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!