3 minutes, so that you learn to distinguish between the self and the python __init__?

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meiguanxi7878/article/details/102691779

init_

On behalf of initialization

You can create a class

Then each instantiation and they will call this function

Let me give you chestnuts

class hehe:
 def __init__(self):
 print('gao')
a=hehe()

This __ the init __ meaning of existence, often assignment, or as a method of starting an internal

To be a metaphor, you create a class, like the establishment of a class, each instance, is a student, but as long as the students entered the class, we must give him a set of corresponding classes to attend classes this book, then this process, send books, assignments, which is done in the init

As for the self, which is to represent a student, a student, each student has a first attribute of each student, each time you run an instance method, or a change in the strength properties of the time, only to change his own, not a student failed the exam The whole class is considered hanging branches, right?

class xuesheng:
 def __init__(self,n):
 self.yuwen=n
 def chengji(self):
 print(self.yuwen)
a=xuesheng(100)
b=xuesheng(20)
a.chengji()
b.chengji()

A student, exam 100 points

A student, exam 20 points

They are, in init inside assignment, and is assigned to the self, it is this example

然后,想查看他们成绩,在调用一个成绩方法,打印出来的时候,就是他们各自,自己的成绩,这个时候,要调用self的属性,如果没有self,就凉凉了。对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

觉得文章还可以的话不妨收藏起来慢慢看,有任何意见或者看法欢迎大家评论!

Guess you like

Origin blog.csdn.net/meiguanxi7878/article/details/102691779