The __init__ and self are two keywords in python, which performs a vital role in the application.

__init__self是python中的两个关键字,在应用程序中起着至关重要的作用。

To begin with, it is important to understand the concept of class and object.

首先,了解类和对象的概念很重要。

Class

In Object-oriented programming, a class is a blueprint for creating objects of a particular data structure, provisioning the initial values for the state, and implementation of a behavior.

在面向对象的编程中,类是用于创建特定数据结构的对象,提供状态的初始值以及实现行为的蓝图。

The user-defined objects are created using the class keyword.

用户定义的对象是使用class关键字创建的。

Object

目的

It is a basic unit of Object-Oriented Programming and each object is an instance of a particular class or subclass with class's methods or procedures and data variables.

它是面向对象编程的基本单元,每个对象都是具有类的方法或过程以及数据变量的特定类或子类的实例。

With the above understanding,

基于以上理解,

__在里面__ (__init__)

__init__ is a reserved method in python classes. It is used to create an object of a class, something like a constructor in Java. This method when called creates an object of the class and it allows the class to initialize the attributes of the class.

__init__是python类中的保留方法。 它用于创建类的对象,类似于Java中的构造函数。 调用此方法时,将创建该类的对象,并允许该类初始化该类的属性。

Example usage of __init__:

__init__的用法示例:

# A Sample class with init method
class Country:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def hello(self):
print('Hello, my name is', self.name)
c = Country('India')
c.hello()

Output

输出量

Hello, my name is India

In the above example, the line c = Country('India') invokes the method __init__ and creates an object c, which can then further invoke the method hello().

在上面的示例中,行c = Country('India')调用方法__init__并创建对象c ,然后可以进一步调用方法hello() 。

自 (self)

The word self is used to represent the instance of the class. Using self, the attributes and the methods of the class can be accessed.

单词self用于表示类的实例。 使用self ,可以访问类的属性和方法。

Example usage of self:

自我用法示例:

class Country:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def hello(self):
print('Hello, my name is', self.name)

Output

输出量

No output

In the above example, name is the attribute of the class Country and it can be accessed by using the self keyword.

在上面的示例中, name是Country类的属性,可以使用self关键字对其进行访问。

翻译自: https://www.includehelp.com/python/what-__init__-and-self-do-in-python.aspx

Python中的__init__和self是做什么的?相关推荐

  1. python方法_详细解读Python中的__init__()方法

    __init__()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是__init__()参数值可以有多种形式. 因为有很多 ...

  2. python中def _init_是什么意思_详细解读Python中的__init__()方法

    __init__()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是__init__()参数值可以有多种形式. 因为有很多 ...

  3. 浅谈 Python 中的 __init__ 和 __new__

    2019独角兽企业重金招聘Python工程师标准>>> 1.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,_ ...

  4. pythoninit作用_简介Python中的__init__的作用

    看到Python中有个函数名比较奇特,__init__我知道加下划线的函数会自动运行,但是不知道它存在的具体意义.. 今天看到<>第11章 面向对象的编程,中这样介绍它:"给C+ ...

  5. Python中的__init__()方法整理中(两种解释)

    解释一:看懂了就不用看第二种了 __init__()方法是Python学习当中重要的基础知识,__init__()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必 ...

  6. python中super().__init__()

    文章目录 super().__ init__ ()有什么用? 1.分别理解super()和 __ init __() 1.1.super() 1.2.__ init __() 1.3.super(). ...

  7. 在Python中什么是闭包?能做什么?

    1.什么是闭包 当我们在外部函数中定义了一个内部函数,并且内部函数能够读取到外部函数内的变量,这种函数我们就称为闭包.简单来说,闭包就是能够读取外部函数内的变量的函数.闭包的架子大概是这样: def ...

  8. 详解Python中def __init__(self)与def __init__(self.参数1、参数2······)的区别

    两者的主要区别 1.初始化参数为空的表述方式不同 2. 初始化附参数的表达方式不同,结构2的self.name一定要指向上面的参数 3. 实例化时的方法不同 def init(self)与def in ...

  9. Python中:self和__init__的含义 + 为何要有self和__init__

    为什么80%的码农都做不了架构师?>>>    本文转自: http://www.crifan.com/summary_the_meaning_of_self_and___init_ ...

最新文章

  1. GDI+ Bitmap与WPF BitmapImage的相互转换
  2. [Everyday Mathematics]20150222
  3. Gamma阶段第九次scrum meeting
  4. centos下安装apache+mysql5.7.13+php5.3.3+phpmyadmin4.0.10
  5. 菜鸟、普通、老鸟程序猿如何写奇数判断?--位操作符妙用
  6. Spring和SpringMVC的父子容器关系
  7. 【CyberSecurityLearning 71】DC系列之DC-2渗透测试(WordPress)
  8. php-v 查看不到版本,解決php -v查看到版本於phpinfo()打印的版本不一致問題
  9. 一个网页设计师应该考虑的9件事
  10. Java 分割字符串
  11. FireWire笔记
  12. 使用UTL_SMTP包发送邮件
  13. 「技术综述」人脸表情识别研究
  14. svn修改ip windows_svn服务地址怎么更换成ip - 卡饭网
  15. 基于 MindStudio 完成 SE-ResNeXt101- PyTorch 模型开发
  16. 共饮维港夜色,TUV南德香港啤酒节精彩落幕
  17. 基于VPB的三维地形建模小结
  18. 程序员业余项目死亡的原因
  19. android zram内存压缩的优缺点
  20. 以mysql为例有几种隔离级别_mysql有几种隔离级别

热门文章

  1. Ubuntu更换gnome桌面环境后不能root登录
  2. bootstracp实现树形列表_Java实现一致性哈希算法,并搭建环境测试其负载均衡特性...
  3. android preferenceactivity 源码,PreferenceActivity PreferenceScreen (3.0/API 11 以前)
  4. python爬虫爬取音乐单曲_Python爬取qq音乐的过程实例
  5. 设置linux拨号服务端,CentOS Linux上搭建PPPoE服务器及拨号设置
  6. V$LICENSE表结构
  7. 高性能MySQL(2)——Schema与数据类型的优化
  8. UVA - 512 ​​​​​​​Spreadsheet Tracking
  9. 在C#中创建DataTable
  10. JavaScript中的arguments对象