__new__和__init__在Python [英] __new__ and __init__ in Python
问题描述
我正在学习Python,到目前为止,我可以告诉下面的事情 __ new __
和 __ init __
:
I am learning Python and so far I can tell the things below about __new__
and __init__
:
-
__ new __
用于创建对象 -
__ init __
是用于对象初始化 c>__init __ 作为
__ new __
返回一个新实例,然后调用__ init __
/ li>
__ new __
适用于不可变对象,因为它们在分配后无法更改。
- 我们可以使用
__ new __
和__ init __ $
__new__
is for object creation__init__
is for object initialization__new__
is invoked before__init__
as__new__
returns a new instance and__init__
invoked afterwards to initialize inner state.__new__
is good for immutable object as they cannot be changed once they are assigned. So we can return new instance which has new state.- We can use
__new__
and__init__
for both mutable object as its inner state can be changed.
但我现在有另一个问题。
But I have another questions now.
- 当我创建一个新实例,例如
a = MyClass(hello,world)
,如何传递这些参数?我的意思是我应该使用
__ init __
和__ new __
结构化类,因为它们是不同的,并且接受任意参数参数。 -
self
关键字在名称方面可以更改为别的?但我想知道cls
在名称方面可以改变为别的,因为它只是一个参数名称?
- When I create a new instance such as
a = MyClass("hello","world")
, how these arguments are passed? I mean how I should structure the class using__init__
and__new__
as they are different and both accepts arbitrary arguments besides default first argument. self
keyword is in terms of name can be changed to something else? But I am wonderingcls
is in terms of name is subject to change to something else as it is just a parameter name?
我做了一些实验如下:
>>> class MyClass(tuple):
def __new__(tuple):
return [1,2,3]
和我如下:
>>> a = MyClass()
>>> a
[1, 2, 3]
虽然我说我要返回 tuple
,这段代码工作正常,并返回 [1,2,3]
。我知道我们传递第一个参数作为我们想要接收的类型,一旦调用 __ new __
函数。我们在谈论新
功能吧?我不知道其他语言的返回类型而不是绑定类型?
Albeit I said I want to return tuple
, this code works fine and returned me [1,2,3]
. I knew we were passing the first parameters as the type we wanted to receive once the __new__
function is invoked. We are talking about New
function right? I don't know other languages return type other than bound type?
我还有其他事情:
>>> issubclass(MyClass,list)
False
>>> issubclass(MyClass,tuple)
True
>>> isinstance(a,MyClass)
False
>>> isinstance(a,tuple)
False
>>> isinstance(a,list)
True
我没有做更多的实验,不明亮,我决定停在那里,决定问StackOverflow。
I didn't do more experiment because the further wasn't bright and I decided to stop there and decided to ask StackOverflow.
我读的SO帖子:
- Python object creation
- Python's use of __new__ and __init__?
推荐答案
如何使用
__ init __
和__ new __
,因为它们不同,并且都接受除默认第一个参数之外的任意参数。
how I should structure the class using
__init__
and__new__
as they are different and both accepts arbitrary arguments besides default first argument.
只有极少的情况,您才需要担心 __ new __
。通常,您只需定义 __ init __
,并让默认 __ new __
将构造函数参数传递给它。
Only rarely will you have to worry about __new__
. Usually, you'll just define __init__
and let the default __new__
pass the constructor arguments to it.
self
但是我想知道cls
是在名称方面可以更改为别的,因为它只是一个参数名称?
self
keyword is in terms of name can be changed to something else? But I am wonderingcls
is in terms of name is subject to change to something else as it is just a parameter name?
两者都是参数名,在语言中没有特殊意义。但是它们的使用在Python社区中是一个非常强的约定;大多数Pythonistas会在这些上下文中永远不会改变 self
和 cls
Both are just parameter names with no special meaning in the language. But their use is a very strong convention in the Python community; most Pythonistas will never change the names self
and cls
in these contexts and will be confused when someone else does.
请注意,您使用 def __new __(tuple)
重新绑定名称 tuple
在构造函数中。当实际实现 __ new __
时,您需要这样做:
Note that your use of def __new__(tuple)
re-binds the name tuple
inside the constructor function. When actually implementing __new__
, you'll want to do it as
def __new__(cls, *args, **kwargs):
# do allocation to get an object, say, obj
return obj
虽然我说我要返回
tuple
,但这个代码工作正常, code> [1,2,3] 。
Albeit I said I want to return
tuple
, this code works fine and returned me[1,2,3]
.
MyClass ()
将具有 __ new __
返回的值。 Python中没有隐式类型检查;程序员有责任返回正确的类型(我们都同意成人 here)。能够返回不同于请求的类型对于实现工厂是有用的:您可以返回所请求类型的子类。
MyClass()
will have the value that __new__
returns. There's no implicit type checking in Python; it's the responsibility of the programmer to return the correct type ("we're all consenting adults here"). Being able to return a different type than requested can be useful for implementing factories: you can return a subclass of the type requested.
这也解释了 issubclass
/ isinstance
你观察到的行为:子类关系来自于你使用 class MyClass(tuple)
, isinstance
反映您从 __新__
返回错误类型。
This also explains the issubclass
/isinstance
behavior you observe: the subclass relationship follows from your use of class MyClass(tuple)
, the isinstance
reflects that you return the "wrong" type from __new__
.
For reference, check out the requirements for __new__
in the Python Language Reference.
编辑:
class Eel(object):
MAX_EELS = 20
n_eels = 0
def __new__(cls, *args, **kwargs):
if cls.n_eels == cls.MAX_EELS:
raise HovercraftFull()
obj = super(Eel, cls).__new__(cls)
cls.n_eels += 1
return obj
def __init__(self, voltage):
self.voltage = voltage
def __del__(self):
type(self).n_eels -= 1
def electric(self):
"""Is this an electric eel?"""
return self.voltage > 0
注意,有更聪明的方式来实现此行为。
Mind you, there are smarter ways to accomplish this behavior.
这篇关于__new__和__init__在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!