[英]Creating an instance of a class without defining the __init__ function
I am relativly new to python and I was wondering if you could create an instance of a class without defining the init explicity. 我相对来说是python的新手,我想知道你是否可以在不定义init明确的情况下创建一个类的实例。 Could I call it something else?
我可以称之为其他吗?
First example - with the init method: 第一个例子 - 使用init方法:
class dog:
def __init__(self,name):
self.name=name
print('My name is',name)
Bob = dog('Bob')
Second example - without the init method: 第二个例子 - 没有init方法:
class dog:
def init_instance(self,name):
self.name = name
print('My name is',name)
Bob = dog('Bob')
In the first example the code works but in the second example I get: 在第一个例子中,代码有效,但在第二个例子中,我得到:
TypeError: object() takes no parameters
TypeError:object()不带参数
So based on this I assume that one has to explicitly call the init method. 所以基于此我假设必须显式调用init方法。 BUT I have seen code where the init method has not been used, how come?
但是我看过没有使用过init方法的代码,为什么会这样?
Every class has an __init__
method. 每个类都有
__init__
方法。 If it doesn't explicitly define one, then it will inherit one from its parent class. 如果它没有明确定义一个,那么它将从其父类继承一个。 In your 2nd example, the class inherits
__init__
and a bunch of other methods (and other non-method attributes) from the base object
class. 在第二个示例中,该类从基础
object
类继承__init__
和一堆其他方法(以及其他非方法属性)。 We can see that via the dir
function: 我们可以通过
dir
函数看到:
class Dog:
def init_instance(self,name):
self.name = name
print('My name is',name)
print(dir(Dog))
output 产量
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'init_instance']
__init__
gets called automatically after the instance is constructed (via the __new__
method), so we might as well use it if we need to initialize our instance. 在构造实例之后(通过
__new__
方法)自动调用__init__
,因此如果我们需要初始化实例,我们也可以使用它。 But we can call your init_instance
explicitly: 但是我们可以显式调用你的
init_instance
:
bob = Dog()
bob.init_instance('Bob')
print(bob.name)
output 产量
My name is Bob
Bob
If you give you class an initializer that isn't named __init__
then it won't get called automatically. 如果你为类提供一个名为
__init__
的初始化程序,那么它将不会被自动调用。 How should Python know that that method is an initializer? Python应该如何知道该方法是初始化器? Although it's customary to make
__init__
the first method in the class definition, that's by no means mandatory, and some people like to put __init__
last . 尽管习惯
__init__
作为类定义中的第一个方法,但这绝不是强制性的,有些人喜欢将__init__
放在最后 。
You said: "I have seen code where the init method has not been used, how come?" 你说:“我看过没有使用过init方法的代码,为什么会这样?” Well, some classes simply don't need their instances to be initialized: their instance attributes are set via various other methods, or by direct assignment in code outside the class definition, eg
bob.color = 'brown'
. 好吧,有些类根本不需要初始化它们的实例:它们的实例属性是通过各种其他方法设置的,或者通过在类定义之外的代码中直接赋值,例如
bob.color = 'brown'
。 Or they inherit a perfectly usable __init__
from a parent class. 或者他们从父类继承了一个完全可用的
__init__
。
init
is nothing else then a method to initially prepare the state of your object. init
就是最初准备对象状态的方法。 In other languages they have similar concepts as Constructors
and it's not necessarily needed. 在其他语言中,它们具有与
Constructors
类似的概念,并不一定需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.