__init__
which usually they don’t fully understand. In this lesson, we will try to understand the use of
__init__
completely with good examples. Let’s get started.
每当初学者开始学习Python编程语言时,他们都会遇到诸如__init__
东西,通常他们并不完全了解。 在本课程中,我们将通过良好的示例尝试完全理解__init__
的用法。 让我们开始吧。
Let’s see a short code snippet and see what we’re trying to understand:
让我们看一个简短的代码片段,看看我们试图理解什么:
class Student(object):
def __init__(self, something):
print("Init called.")
self.something = something
def method(self):
return self.something
my_object = Student('Jetty')
What does the __init__
method do? Why is it necessary? Let’s find out.
__init__
方法有什么作用? 为什么有必要? 让我们找出答案。
When a new instance of a python class is created, it is the __init__
method which is called and proves to be a very good place where we can modify the object after it has been created.
当创建一个新的python类实例时,将调用__init__
方法,事实证明这是一个非常好的地方,我们可以在创建对象后修改该对象。
This means that when we create a new instance of the class like:
这意味着当我们创建类的新实例时,如下所示:
my_object = Student('Jetty')
In above snippet, when we called Student with ‘Jetty’ (which could be actually anything), it gets passed to the __init__
function as the argument, Jetty. Let’s try to run this script now:
在上面的代码片段中,当我们用“ Jetty”(实际上可以是任何东西)调用Student时,它将作为参数Jetty传递给__init__
函数。 让我们尝试现在运行此脚本:
Actually yes. __init__
is an oop construct. __init__
is the constructor for a class. Just like mentioned above, the __init__
method is called as soon as the memory for the object is allocated. Let’s see what we did above in our snippet:
其实,是。 __init__
是一个oop构造。 __init__
是类的构造函数。 就像上面提到的那样,一旦分配了对象的内存,就会调用__init__
方法。 让我们看看上面片段中的操作:
def __init__(self, something):
self.something = something
Using self
is important because if you don’t and implement your method like:
使用self
很重要,因为如果您不这样做,则可以实现以下方法:
def __init__(self, something):
_something = something
The something
parameter would be stored in variables on the stack and would be discarded as soon as the __init__
method goes out of scope.
something
参数将存储在堆栈中的变量中,并且当__init__
方法超出范围时将被丢弃。
When we have a class inheriting from a superclass, __init__
method works the same way. Let us try to demonstrate what happens when we try to initialise a child class:
当我们有一个从超类继承的类时, __init__
方法的工作方式相同。 让我们尝试演示在尝试初始化子类时会发生什么:
class User(object):
def __init__(self, something):
print("User Init called.")
self.something = something
def method(self):
return self.something
class Student(User):
def __init__(self, something):
User.__init__(self, something)
print("Student Init called.")
self.something = something
def method(self):
return self.something
my_object = Student('Jetty')
In above code, when we initialised the Student object, this will be the output which is created when we ran the above program:
在上面的代码中,当我们初始化Student对象时,这将是运行上述程序时创建的输出:
因此,在子类之前,将调用父类的init。 您可以通过修改父类或子类的init调用顺序来控制它。 在python继承中阅读更多内容。
To summarise, python __init__
is what is called as a constructor in other OOPs languages such as C++ and Java. The basic idea behind this is, it a special method which is automatically called when an object of that Class is created.
总而言之,python __init__
在其他OOP语言(例如C ++和Java)中称为构造函数。 这背后的基本思想是,它是一种特殊的方法,当创建该Class的对象时会自动调用该方法。
翻译自: https://www.journaldev.com/18397/python-class-init