栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python类初始化– Python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python类初始化– Python

Python类初始化 (Python class init)


Whenever a beginner starts learning the Python programming language, they come across something like __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__的用法。 让我们开始吧。

了解python类的初始化函数 (Understanding python class init function)

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__方法有什么作用? 为什么有必要? 让我们找出答案。

python init方法有什么作用? (What does the python init method do?)

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__函数。 让我们尝试现在运行此脚本:

__init__是构造函数吗? (Is __init__ the constructor?)

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__方法超出范围时将被丢弃。

__init__如何与继承一起使用? (How __init__ works with Inheritance?)

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:


So, before the child class, the parent’s class init was called. You can control this by modifying the order in which the init is called for a parent or a child class. Read more at python inheritance.

在上面的代码中,当我们初始化Student对象时,这将是运行上述程序时创建的输出:

因此,在子类之前,将调用父类的init。 您可以通过修改父类或子类的init调用顺序来控制它。 在python继承中阅读更多内容。

结论 (Conclusion)

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号