根据文件,
__new__()是一个静态方法(特殊的,因此您不需要声明它),它将请求实例的类作为其第一个参数.
它显然不是类方法,但通常看起来像一个,除了手动调用的任何客户端__new__需要显式传入类参数.例如:
>>> str.__new__()
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
str.__new__()
TypeError: str.__new__(): not enough arguments
>>> str.__new__(str)
''
Run Code Online (Sandbox Code Playgroud)
但是替代对象创建API -例如,所有八个备选datetime构造 - 是平时classmethodS,SO是datetime.now()按预期工作.
为什么这样__new__设置?
| 归档时间: |
|
| 查看次数: |
921 次 |
| 最近记录: |