Python类初始化– Python __init __()函数

Python类初始化 (Python class init)

python class init, python __init__ function

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:

python class init

在上面的代码片段中,当我们用“ 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:

python init super class inheritance

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

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值